Need help figuring out why this nested function is not working
2 views (last 30 days)
Show older comments
function [imgstruct,xyzuvwcrm] = IMGHandle(inputArg1)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
files = inputArg1;
count = 1;
for k = 1:length(files)
%[h,d]=svecread(files(k).name)
varargout = svec2(files(k).name); %Pass files into function to get img struc
% for i = 1:8
% table(:,i) = varargout(:,:,i);
% end
%Adds name of file to struct
xyzuvwcrm(k).name = files(k).name;
for i = 1:8
for x = 1:54
for y = 1:73
xyzuvwcrm(k).data(count,i) = varargout(x,y,i); %Read matrix val to table
count = count +1; %Counting pixels in the image
end
end
count = 1;
end
%Compute the magnitude of u,v,w and insert into column 9 of xyzuvwcrm
for l = 1:size(xyzuvwcrm(k).data,1)
xyzuvwcrm(k).data(l,9) = sqrt(sum((xyzuvwcrm(k).data(l,4:6)).^2));
end
%Creates the Image pixel structure
xyzuvwcrm(k).data = array2table(xyzuvwcrm(k).data,...
'VariableNames',["x mm","y mm","z mm","u m/s","v m/s","w m/s",...
"CHC","R Err","Magnitude"]);
imgstruct(k).name = files(k).name;
imgstruct(k).data = varargout ;%(:,:,i)
end
function varargout = svec2(varargin)
msg = nargchk(1,3,nargin); if ~isempty(msg), error(msg), end;
% Defaults:
if nargin < 3
varargin{3} = 8; % default columns value (13/08/01)
if nargin < 2
varargin{2} = 1; % default number of header lins
end
end
% Assign variables
name = varargin{1};
% append an extension if the user neglected to include it
headerlines = 1;
columns = 8;
% this seems like it would cause as many problems as it might fix.
if isempty(strfind(lower(name),'.v3d'))
name = strcat(name,'.v3D');
end
% Read the header
fid = fopen(name,'r');
if fid<0
error('File not found');
end
hdr = fgetl(fid);
fclose(fid);
% Parse the header
% i'm terrible at using regex safely,
% so take this with a grain of salt
hdr = lower(hdr);
i = regexp(hdr,'i=(\d+)','tokens');
i = str2double(i{1}{1});
j = regexp(hdr,'j=(\d+)','tokens');
j = str2double(j{1}{1});
k = regexp(hdr,'k=(\d+)','tokens');
k = str2double(k{1}{1});
% read everything using a convenience tool
data = readmatrix(name,'filetype','text', ...
'expectednumvariables',columns, ...
'numheaderlines',headerlines);
badind = find(data>9e9);
if ~isempty(badind), data(badind) = 0; warning(sprintf('Bad %d points',length(badind))); end;
% then reshape
% i don't know which way this is supposed to be oriented
data = reshape(data,i,j,[]);
data = permute(data,[2 1 3]);
if nargout == 1
varargout{1} = data;
elseif nargout == 2
varargout{1} = hdr;
varargout{2} = data;
elseif nargout == 4
varargout{1} = hdr;
varargout{2} = data;
varargout{3} = str2num(i);
varargout{4} = str2num(j);
varargout{5} = str2num(k);
else
warning('Wrong number of outputs') ;
end
end
end
As you can see from the function above IMGHandle is the main function which uses the function svec2 defined within it. I have made sure that all files are within the same folder and this is the current folder. Have restarted matlab to try to update cache etc.
Currently the output I am getting when trying to pass a struct into IMGHandle is the following:
Error using pivdatareadertest>IMGHandle/svec2
File not found
Error in pivdatareadertest>IMGHandle (line 254)
varargout = svec2(files(k).name); %Pass files into function to get img struc
Error in pivdatareadertest (line 55)
[imgstruct,xyzuvwcrm] = IMGHandle(Class4FS20HZ);
Thank you.
0 Comments
Accepted Answer
Stephen23
on 5 May 2024
Edited: Stephen23
on 5 May 2024
"File not found"
You are not providing FOPEN and READMATRIX with the filepath, so they cannot find the file on your computer. Presuming that INPUTARG1 is actually the structure returned from DIR, then you should replace
svec2(files(k).name);
with
svec2(fullfile(files(k).folder,files(k).name));
Note that your use of VARARGOUT when calling SVEC2 is not a cell array of function output arrays (of the function IMGHANDLE). To avoid confusion (e.g. your own) you should rename it to e.g. DATA.
As far as I can tell you are not accessing any variables directly from the parent workspace, so nesting the function SVEC2 serves no purpose. It might as well be a simple local function.
6 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!