Matlab 文件读写(I/O)和字符串操作常用命令

本文详细介绍了Matlab中的文件I/O操作,包括fopen、fclose、load、fread、fscanf等,以及字符串操作,如length、strcat、strcmp、strrep等,帮助读者更好地理解和运用这些功能。

Import Text Data Files with Low-Level I/O
基本函数:
fscanf: 读写在text or ASCII file中的数据
fgetl: 一次读一行不保留换行符号
fgets: 一次读一行保留换行符号
fread:读二进制文件,与fwrite 对应
fscanf:


A = fscanf(fileID, format) reads and converts  data from a text file into array A  in column order. To convert, fscanf  uses the format and the encoding scheme associated with the file. To set the encodings cheme, use fopen.  The fscanf function reapplies the format throughout the entire file, and positions the file pointer at the end-of-file marker. If fscanf cannot match the format to the data, it reads only the portion that matches into A and stops processing.

A = fscanf(fileID, format, sizeA)  reads sizeA elements into A, and positions the file pointerafter the last element read. sizeA canbe an integer, or can have the form [m,n].

sizeA

example:




Reading Data in a Formatted Pattern
To import text files that importdata and textscan cannotread, consider using fscanf。
fscanf: 自主设计格式。


Reading Data Line-by-Line
MATLAB provides two functions that read lines from filesand store them in string vectors: fgetl and fgets. The fgets function copies the newline characterto the output string, but fgetl does not.
exmaple:


Testing for End of File (EOF)



2014.04.04


Add:
dir(): 查询文件夹中的文件,可加后缀查找指定格式的文件。如 dir(['fk\' , '*.jpg'])表示查找fk文件夹下后缀为‘.jpg’的文件
mkdir():创建文件夹。 mkdir('fj') 在当前目录下创建名为fj文件夹;mkdir('fj\fi')表示当前路径下的fj文件夹里创建fi子文件夹。
rmdir( 'fl'): 用于删除文件夹
copyfile( srcpath , despath): 拷贝文件或者文件夹

一、matlab对路径的操作
1. filesep
返回当前平台目录分隔符,window: '\' ; linux : '/'

2. fullfile
<think> 我们正在讨论如何在C#中用字符串表示路径。根据之前的讨论,我们知道在C#中表示路径字符串时,需要注意路径分隔符的处理以及字符串的表示方式。以下是几种常见的方法: 1. **使用普通字符串(转义反斜杠)**: 在Windows系统中,路径分隔符是反斜杠`\`,而在C#字符串中,反斜杠是一个转义字符。因此,我们需要使用两个反斜杠`\\`来表示一个反斜杠。 例如:`string path = "C:\\\\Users\\\\Documents\\\\file.txt";` 2. **使用逐字字符串(使用`@`前缀)**: 为了避免转义反斜杠的麻烦,C#提供了逐字字符串,即在字符串前加上`@`符号。这样,字符串中的反斜杠就不再需要转义。 例如:`string path = @"C:\Users\Documents\file.txt";` 3. **使用正斜杠`/`**: 在C#中,正斜杠`/`也可以用作路径分隔符,因为Windows API通常能够接受正斜杠。这样,我们就不需要转义,可以直接使用。 例如:`string path = "C:/Users/Documents/file.txt";` 4. **使用`Path.Combine`构建路径**: 当需要将多个部分组合成一个完整的路径时,推荐使用`Path.Combine`方法,它会自动处理路径分隔符。注意,在组合之前,每一部分都不应包含路径分隔符(除了根路径部分)。 例如:`string fullPath = Path.Combine("C:", "Users", "Documents", "file.txt");` 5. **使用`Path`类的其他方法**: `Path`类还提供了其他有用的方法,例如`Path.GetFullPath`(获取绝对路径)、`Path.GetDirectoryName`(获取目录名)等。 ### 示例代码 下面是一个示例,展示不同方式表示路径字符串: ```csharp using System; using System.IO; class Program { static void Main() { // 方法1: 使用转义反斜杠 string path1 = "C:\\\\Users\\\\Documents\\\\file.txt"; Console.WriteLine(path1); // 方法2: 使用逐字字符串 string path2 = @"C:\Users\Documents\file.txt"; Console.WriteLine(path2); // 方法3: 使用正斜杠 string path3 = "C:/Users/Documents/file.txt"; Console.WriteLine(path3); // 方法4: 使用Path.Combine string path4 = Path.Combine("C:", "Users", "Documents", "file.txt"); Console.WriteLine(path4); // 输出: C:\Users\Documents\file.txt // 注意:Path.Combine的第一个参数如果是驱动器根目录(如"C:"),则后面的部分会直接拼接。 // 但通常,我们会使用根路径(如@"C:\")而不是驱动器字母加冒号("C:"),因为"冒号"后的第一个反斜杠会被认为是根目录。 // 更常见的用法是: string root = @"C:\"; string fullPath = Path.Combine(root, "Users", "Documents", "file.txt"); Console.WriteLine(fullPath); // 输出: C:\Users\Documents\file.txt } } ``` ### 注意事项 - 在跨平台开发中(如使用.NET Core),路径分隔符可能会不同(Linux/macOS使用`/`)。因此,推荐使用`Path.Combine``Path`类的方法来确保兼容性。 - 避免手动拼接路径字符串,因为容易出错(比如重复分隔符或缺少分隔符)。 - 在需要处理用户输入的路径时,使用`Path`类的方法可以增加安全性,避免路径注入等问题。 ### 引用说明 在C#中表示路径字符串时,我们可以使用转义字符或逐字字符串来正确书写反斜杠,如引用[1][2]所提到的字符串处理方式[^1][^2]。另外,在涉及到跨平台或与本地代码交互时(如引用[3]中调用C++ DLL传递字符串路径),也需要注意字符串的编码表示方式[^3]。 ### 相关问题 1. 在C#中,如何将相对路径转换为绝对路径? 2. 如何处理跨平台(Windows/Linux)的路径字符串? 3. 在C#中,如何安全地处理用户输入的路径以防止路径遍历攻击(Path Traversal)? </think> ### 在C#中用字符串表示路径的方法 在C#中表示路径字符串时,需特别注意**路径分隔符****字符串格式**的处理。以下是核心方法注意事项: #### 1. **使用逐字字符串(推荐)** 用 `@` 前缀创建逐字字符串,避免反斜杠转义问题: ```csharp string winPath = @"C:\Program Files\App\config.ini"; // Windows路径 string linuxPath = @"/home/user/documents/report.pdf"; // Linux路径 ``` - ✅ 优点:无需转义反斜杠`\`,路径可读性高[^2] - ⚠️ 注意:路径中的引号需双写(如 `@"C:\"quoted\"folder"`) #### 2. **使用转义字符** 普通字符串需用双反斜杠`\\`转义: ```csharp string path = "C:\\\\User\\\\Desktop\\\\test.txt"; // 等价于 C:\User\Desktop\test.txt ``` - ❌ 缺点:可读性差且易出错(每1个`\`需写为`\\`) #### 3. **跨平台路径处理** 使用 `Path` 类确保兼容性: ```csharp // 组合路径(自动处理分隔符) string fullPath = Path.Combine("C:", "Data", "2024", "file.csv"); // 获取系统分隔符 char sep = Path.DirectorySeparatorChar; // Windows返回'\',Linux返回'/' ``` #### 4. **特殊字符处理** 对空格等特殊字符需额外处理: ```csharp // 空格路径需用引号包裹 string cmd = $"/C move \"{sourcePath}\" \"{targetPath}\""; Process.Start("cmd.exe", cmd); ``` #### 5. **路径规范化** 使用 `Path.GetFullPath()` 标准化路径: ```csharp string rawPath = "C:/Data//Sub/../App"; string normalized = Path.GetFullPath(rawPath); // 输出 C:\Data\App ``` ### ⚠️ 关键注意事项 1. **跨平台兼容性** - Windows 支持正斜杠 `/` 反斜杠 `\` - Linux/macOS 仅支持正斜杠 `/` - 推荐始终用 `Path.Combine()` 代替手动拼接 2. **相对路径基准** 相对路径基于 **当前工作目录**(可通过 `Environment.CurrentDirectory` 获取) 3. **网络路径表示** ```csharp string networkPath = @"\\Server\Shared\file.txt"; // UNC路径 ``` 4. **特殊前缀处理** ```csharp string devicePath = @"\\.\PhysicalDrive1"; // 物理设备路径 string longPath = @"\\?\C:\超长路径..."; // 超长路径前缀 ``` ### 相关问题 1. 如何在C#中安全处理用户输入的路径以防止目录遍历攻击? 2. 使用 `Path` 类时,路径字符串中有前导/尾随空格会产生什么影响? 3. 在跨平台应用中,如何统一处理WindowsLinux的路径大小写敏感问题?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值