MATLAB newbie: problem reading in file when the fi

2019-08-29 05:50发布

问题:

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.



标签: matlab fopen