[英]How to input string in a Matlab function
I want to write a function that loads a text file and plots its content with time.我想编写一个加载文本文件并随时间绘制其内容的函数。 I have 20 text files so I want to be able to choose from them.
我有 20 个文本文件,所以我希望能够从中进行选择。
My current not working code:我当前不工作的代码:
TextFile is a generic variable text123.txt is the actual name of one of the files i want to load TextFile 是一个通用变量 text123.txt 是我要加载的文件之一的实际名称
function []= PlotText(TextFile)
text(1,:)=load('text123.txt') ;
t=0:10;
plot(t,text)
end
I appreciate any help!!我感谢任何帮助!
use importdata instead of load with appropriate delimiter.使用 importdata 而不是 load 带有适当的分隔符。 I assume you used Tab.
我假设您使用了 Tab。
filename = 'num.txt';
delimiterIn = '\t';
text = importdata(filename,delimiterIn)
t=1:10;
plot(t,text);
Firstly, you can also use dlmread if your file contains only numeric data separated by the same symbol (called a delimiter) such as a comma (,), semicolon (;), space ( ), or tab ( ).首先,如果您的文件仅包含由相同符号(称为分隔符)分隔的数字数据,例如逗号 (,)、分号 (;)、空格 ( ) 或制表符 ( ),您也可以使用 dlmread。 This would look like:
这看起来像:
function []= PlotText(TextFile)
text(1,:)=dlmread('text123.txt');
t=0:10;
plot(t,text)
end
Keep in mind that your code is written in a way that expects the contents of text123.txt to have 11 values in a single row.请记住,您的代码的编写方式要求 text123.txt 的内容在一行中包含 11 个值。 Also, if you are using multiple files, then I suggest having the file name be another input to the function:
此外,如果您使用多个文件,那么我建议将文件名作为函数的另一个输入:
function []= PlotText(TextFile,filename)
text(1,:)=load(filename) ;
t=0:10;
plot(t,text)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.