MATLAB - Load data file with a string file name

自作多情 提交于 2019-12-11 04:32:34

问题


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*')

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.

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?

Thank you!


回答1:


try using this instead:

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*')


来源:https://stackoverflow.com/questions/13692907/matlab-load-data-file-with-a-string-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!