MATLAB - 加载文件名存储在字符串中的文件
我正在使用 MATLAB 处理文件中的数据。我正在编写一个程序,它接受用户的输入,然后在绘制它们的目录中找到特定文件。文件命名为:
{名称}U{速率}
{name} 是表示计算机名称的字符串。 {rate} 是一个数字。这是我的代码:
%# get user to input name and rate
NET_NAME = input('Enter the NET_NAME of the files: ', 's');
rate = input('Enter the rate of the files: ');
U = strcat(NET_NAME, 'U', rate)
load U;
Ux = U(:,1);
Uy = U(:,2);
目前有两个问题:
当我使用“hello”、“U”执行
strcat
且速率为 50 时,U 将存储“helloU2” - 如何我让strcat
正确附加 {rate}?加载线 - 如何取消引用 U,以便加载尝试加载存储在 U 中的字符串?
加载
非常感谢!
I am using MATLAB to process data from files. I am writing a program that takes input from the user and then locates the particular files in the directory graphing them. Files are named:
{name}U{rate}
{name} is a string representing the name of the computer. {rate} is a number. Here is my code:
%# get user to input name and rate
NET_NAME = input('Enter the NET_NAME of the files: ', 's');
rate = input('Enter the rate of the files: ');
U = strcat(NET_NAME, 'U', rate)
load U;
Ux = U(:,1);
Uy = U(:,2);
There are currently two problems:
When I do the
strcat
with say 'hello', 'U', and rate is 50, U will store 'helloU2' - how can I getstrcat
to append {rate} properly?The load line - how do I dereference U so load tries to load the string stored in U?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
米哈伊尔的上述评论解决了您眼前的问题。
选择文件的更用户友好的方式:
Mikhail's comment above solves your immediate problem.
A more user-friendly way of selecting a file:
除了像 Mikhail 建议的那样使用 SPRINTF 之外,您还可以通过首先使用 NUM2STR 和 INT2STR:
U
中的字符串的一个问题是该文件必须位于 MATLAB 路径 或当前目录中。否则,变量NET_NAME
必须包含完整或部分路径,如下所示:Amro 建议使用UIGETFILE 是理想的选择,因为它可以帮助您确保拥有完整且正确的文件路径。
In addition to using SPRINTF like Mikhail suggested, you can also combine strings and numeric values by first converting the numeric values to strings using functions like NUM2STR and INT2STR:
One issue with the string in
U
is that the file has to be on the MATLAB path or in the current directory. Otherwise, the variableNET_NAME
has to contain a full or partial path like this:Amro's suggestion of using UIGETFILE is ideal because it helps you to ensure you have a complete and correct path to the file.