Need help figuring out why this nested function is not working
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.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. 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. functions MATLAB Answers — New Questions