MATLAB newbie: problem reading in file when the file name is stored in a string

a 夏天 提交于 2020-01-06 05:10:13

问题


I am using Matlab to read in and process calculation results. I use fopen.

My problem is that I currently have to specify a path to each file each time I need to use it in my processing code. For example, this works:

fid = fopen('/Users/me/Desktop/Result1/velocity.tbl', 'r+');
liqmass = textscan(fid, '%f %*f %*f %*n %f %*n %*n %*n %*n %*n %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f', 'headerlines', 1, 'delimiter', ',', 'CollectOutput', 1);
fclose(fid);

But I want to do this:

velocityOut = '/Users/me/Desktop/Result1/velocity.tbl';  % Specify a path once in an easy-to-reach place
fid = fopen(velocityOut, 'r+');
    liqmass = textscan(fid, '%f %*f %*f %*n %f %*n %*n %*n %*n %*n %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f', 'headerlines', 1, 'delimiter', ',', 'CollectOutput', 1);
    fclose(fid);

However, I get the following error: ??? Undefined function or variable 'velocityOut'.

I can't figure out why it isn't working. I would be very grateful if someone could point out my mistake. Thank you.


回答1:


Your code above is correct, and should work fine. Perhaps you might not have evaluated velocityOut before trying to use it in fopen. So if you are working from the command window, you'll need to evaluate it first, or if it's in a script, it should be defined prior to being used in fopen.



来源:https://stackoverflow.com/questions/6484942/matlab-newbie-problem-reading-in-file-when-the-file-name-is-stored-in-a-string

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