MATLAB - 加载文件名存储在字符串中的文件

发布于 2024-08-21 22:52:39 字数 625 浏览 2 评论 0原文

我正在使用 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);

目前有两个问题:

  1. 当我使用“hello”、“U”执行 strcat 且速率为 50 时,U 将存储“helloU2” - 如何我让 strcat 正确附加 {rate}?

  2. 加载线 - 如何取消引用 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:

  1. When I do the strcat with say 'hello', 'U', and rate is 50, U will store 'helloU2' - how can I get strcat to append {rate} properly?

  2. The load line - how do I dereference U so load tries to load the string stored in U?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-08-28 22:52:39

米哈伊尔的上述评论解决了您眼前的问题。

选择文件的更用户友好的方式:

[fileName,filePath] = uigetfile('*', 'Select data file', '.');
if filePath==0, error('None selected!'); end
U = load( fullfile(filePath,fileName) );

Mikhail's comment above solves your immediate problem.

A more user-friendly way of selecting a file:

[fileName,filePath] = uigetfile('*', 'Select data file', '.');
if filePath==0, error('None selected!'); end
U = load( fullfile(filePath,fileName) );
初雪 2024-08-28 22:52:39

除了像 Mikhail 建议的那样使用 SPRINTF 之外,您还可以通过首先使用 NUM2STRINT2STR

U = [NET_NAME 'U' int2str(rate)];
data = load(U);  %# Loads a .mat file with the name in U

U 中的字符串的一个问题是该文件必须位于 MATLAB 路径 或当前目录中。否则,变量 NET_NAME 必须包含完整或部分路径,如下所示:

NET_NAME = 'C:\My Documents\MATLAB\name';  %# A complete path
NET_NAME = 'data\name';  %# data is a folder in the current directory

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:

U = [NET_NAME 'U' int2str(rate)];
data = load(U);  %# Loads a .mat file with the name in U

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 variable NET_NAME has to contain a full or partial path like this:

NET_NAME = 'C:\My Documents\MATLAB\name';  %# A complete path
NET_NAME = 'data\name';  %# data is a folder in the current directory

Amro's suggestion of using UIGETFILE is ideal because it helps you to ensure you have a complete and correct path to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文