MATLAB新手:将文件名存储在字符串中时读取文件时出现问题 [英] MATLAB newbie: problem reading in file when the file name is stored in a string
问题描述
我正在使用Matlab读取和处理计算结果.我用的是fopen.
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);
但是我想这样做:
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);
但是,出现以下错误: ???未定义的函数或变量"velocityOut".
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.
推荐答案
您上面的代码是正确的,应该可以正常工作.尝试在fopen
中使用velocityOut
之前,可能尚未评估过它.因此,如果您是在命令窗口中工作,则需要先对其进行评估,或者如果它在脚本中,则应在之前将其定义为要在fopen
中使用.
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新手:将文件名存储在字符串中时读取文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!