I want to define my function in this manner but how do I get it to work?

2 views (last 30 days)
function [xstruct,ystruct,zstruct,csvData] = JoinStruct(1,2)
tic;
% Input 1 must be named one of {SLB,AB,CCB,DCB,YJS};
% Input 2 must be named one of {SLB2,AB2,CCB2,DCB2,YJS2}
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if numel(JoinStruct) ~= 2
error('The number of inputs must only be two, check number of inputs');
end
if 1 ~= (SLB || AB || CCB || DCB || YJS)
error('Input field 1 is not of the required format, check input 1');
end
if 2 ~= (SLB2 || AB2 || CCB2 || DCB2 || YJS2)
error('Input field 2 is not of the required format, check input 2');
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if 1 == SLB
load('SLB.csv');
fSLB = fopen('SLB.csv', 'rt');
SLB = [SLB(:,1:1:3);SLB(end,4:1:6)];
xslbp = SLB(:,1);
yslbp = SLB(:,2);
zslbp = SLB(:,3);
fclose(fSLB);
elseif 1 == AB
load('AB.csv');
etc... for CCB,DCB,YJS
The error I have is 'Undefined function or variable 'SLB''. I sort of know why this is happening, I have not said what SLB is. I want to have this program in the manner that there are two input fields, 1 and 2, and they can take values given in the curly braces in the comments under tic. Depending on the combination of inputs, say (SLB,AB), that; if 1 == SLB, load that file, assign some matrices from reading the file, elseif 1 == AB load that file instead. The same with 2, load SLB2, elseif load AB2 etc. How can I make the SLB, SLB2 etc recognised as variables for this to work?
Thankyou.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Dec 2015
function [xstruct,ystruct,zstruct,csvData] = JoinStruct(field1, field2)
tic;
% Input 1 must be one of {'SLB', 'AB', 'CCB', 'DCB', 'YJS'};
% Input 2 must be named one of {'SLB2', 'AB2', 'CCB2', 'DCB2', 'YJS2'}
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if nargin ~= 2
error('The number of inputs must only be two, check number of inputs');
end
if ~ismember(field1, {'SLB', 'AB', 'CCB', 'DCB', 'YJS'} )
error('Input field 1 is not of the required format, check input 1');
end
if ~ismember(field2, {'SLB2', 'AB2', 'CCB2', 'DCB2', 'YJS2'} )
error('Input field 2 is not of the required format, check input 2');
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
if strcmp(field1, 'SLB')
load('SLB.csv');
fSLB = fopen('SLB.csv', 'rt');
SLB = [SLB(:,1:1:3);SLB(end,4:1:6)];
xslbp = SLB(:,1);
yslbp = SLB(:,2);
zslbp = SLB(:,3);
fclose(fSLB);
elseif strcmp(field1, 'AB')
load('AB.csv');
etc... for CCB,DCB,YJS
The user would need to pass in strings, not just names. For example, it could be called with
[a, b, c] = JoinStruct('SLB', 'AB2');
Note: you are not using fSLB other than to open and close the SLB.csv file. You do not need to do that as you already loaded the data with the load() statement.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!