开发者

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

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开发者_C百科 %*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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜