HELP with Undefined variable, size of the indicated variable changes with each loop iteration.

Hello, i have been running a code during some time and it worked. However now, i am trying to run it and it does not run because of this error message:
??? Undefined function or variable 'files'.
Error in ==> after_all at 20 coord = regexp(files,'\d+(\.\d+)?','match'); % It extracts the coordinates from the name of each file
The problem is the variable files (The size of the indicated variable or array appears to be changing with each loop iteration) it says...
Can anyone help me, please?
This is my code:
clear all; % clear all variables
close all; % close all plot windows
clc; % clear the command window
workfolder='C:\Users\Jorge\Desktop\Airfoil_BASELINE_Measurements\Airfoil_BASELINE_Measurements\Re140k00deg'; % It defines the folder (rpm or Re) to run
folders=[ % It indicates that in the folder selected we are interested in the Hot Wire measurements
{'cdaq2mod2_ai0-hw\'}
{'cdaq2mod2_ai1-hw\'}
];
file_data=dir([workfolder folders{1} 'x*.dat']); %Inside the hot wire measurements we look for all the files with an "x" in its name
for i=1:length(file_data);
files(i,1)={file_data(i).name}; % It creates a variable with all the names of the files
end
% files=[
% {'x_+0.000mm_y_+0.000mm_z_+0.000mm.dat'}
% {'x_+0.000mm_y_+5.000mm_z_+0.000mm.dat'}
% ];
coord = regexp(files,'\d+(\.\d+)?','match'); % It extracts the coordinates from the name of each file
xyzCoords = cell2mat(cellfun(@(x)str2num(char(x))',coord,'UniformOutput',false)); % It organize the coordinates properly

 采纳的回答

The error is Undefined function or variable 'files'. files is defined inside your for loop. So if it is undefined after the loop that means your loop didn't execute. The only reason for your loop not to execute is that length(file_data) is 0, i.e. no file was found.
Morale of the story: when interacting with the filesystem (or any data outside your program control, like user input), always check that you're getting what you're expecting. In this case, do check that there is at least one file.
Note: You don't need a loop to transform the structure returned by dir into a cell array of filenames:
files = {file_data.name};
works just as well. It would also have avoided the undefined variable error in regexp (which would just have returned an empty cell array. Most likely, another error would occur later because you haven't designed your code to cope with no file being found.

4 个评论

Okay Guillaume, so the problem is file_data, but i do not see why there is a problem there.
workfolder='C:\Users\Jorge\Desktop\Airfoil_BASELINE_Measurements\Airfoil_BASELINE_Measurements\Re140k00deg'; % It defines the folder (rpm or Re) to run
folders=[ % It indicates that in the folder selected we are interested in the Hot Wire measurements
{'cdaq2mod2_ai0-hw\'}
{'cdaq2mod2_ai1-hw\'}
];
file_data=dir([workfolder folders{1} 'x*.dat']);
file_data is a variable that is well referenced, i have checked the workfolder and the folders and everything exits and is full of files so why does length(file_data) is 0 or not found???
Is it a problem with the location of "folders"?, or with the dir function?
How is it possible that it sometimes work and sometimes does not?
Please, any idea would be helpful
I suspect you're missing a trailing slash at the end of workfolder. As a result, you're searching the content of the folder Re140k00degcdaq2mod2_ai0-hw.
The best way to avoid this sort of problem is not to use string concatenation, but path combining function, that is fullfile, which adds '/' when needed:
file_data = dir(fullfile(workfolder, folders{1}, 'x*.dat')); %what about folders{2:end}?
Again, all this would become obvious if you design your code to report on problem, e.g.:
fullpath = fullfile(workfolder, folders{1});
filepattern = 'x*.dat'
file_data = dir(fullfile(fullpath, filepattern));
if isempty(file_data)
error('no file matching pattern '%s' was found in folder '%s', filepattern, fullpath);
end
Oh my god. It was the trailing slash all the time. Thank you very much for your comments.

请先登录,再进行评论。

更多回答(1 个)

The variable files is a cell array, and you are trying to refer it as a matrix. You should use files{i} instead of files(i,1) when you want to assign it:
for i=1:length(file_data);
files{i} = file_data(i).name;
end

5 个评论

Thank you but i have tried that way and the error is still the same :s
I tried this code, and it works:
file_data = dir('*.*');
for i=1:length(file_data);
files{i} = file_data(i).name;
end
What is the error and where do you get it?
The error is still the same at the same line, :
??? Undefined function or variable 'files'.
Error in ==> after_all at 20 coord = regexp(files,'\d+(\.\d+)?','match'); % It extracts the coordinates from the name of each file
I can not understand why the program says now that "files" variable is not defined when i have been running it for a long time and it never was a problem.
@Alessandro,
y{i} = x
and
y(i) = {x}
are equivalent, when y does not exists or is already defined as a cell array.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by