MATLAB-使用字符串文件名加载数据文件 [英] MATLAB - Load data file with a string file name

查看:563
本文介绍了MATLAB-使用字符串文件名加载数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Matlab程序,该程序加载在另一个C ++程序中创建的数据文件.

I am writing a Matlab program that loads a data file created in another C++ program.

    planet = input('What is the name of your planet? ', 's')
    data_file = strcat(planet, '.dat')
    load(data_file);
    data_file;
    x = data_file(:,1);
    y = data_file(:,2);
    plot (x,y,'r*')

程序将行星的名称作为用户输入,然后将".dat"连接到行星名称的末尾.例如,这给出了"earth.dat",这是另一个C ++程序创建的文件的名称.

The program takes the name of the planet as the user input, then concatenates ".dat" to the end of the planet name. This gives, for example, "earth.dat," which is the name of the file created by the other C++ program.

我确保正在加载的数据文件在正确的文件夹中;但是,当我运行该程序时,MATLAB仍然出现错误.

I have made sure that the data file being loaded is in the correct folder; however, MATLAB still gives an error when I run the program.

加载此文件的正确命令是什么?

What is the correct command for loading this file?

谢谢!

推荐答案

尝试使用此代替:

planet = input('What is the name of your planet? ', 's')
filename=[num2str(planet) '.dat'];
data_file=load(filename);
x = data_file(:,1);
y = data_file(:,2);
plot (x,y,'r*')

这篇关于MATLAB-使用字符串文件名加载数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆