Functions in If Statements

5 ビュー (過去 30 日間)
Nat Person
Nat Person 2021 年 1 月 15 日
編集済み: Jan 2021 年 1 月 16 日
I have two versions of code separated by an if statement. The if statement checks the version of Matlab that the user is running and executes based on that. So the two versions of code are incompatible with the other version, so I have to put each block of code in a function. Here's my problem: I have no idea how to properly execute the function in the main program. Here is some example code
Main Block of code
v= ver('MATLAB');
if v.Release =='(R2012a)'
for n= 1:FMn %cycle through files in the directory
finalname = FMdir(n).name;
end
start = 1; %start at one
out = n; %stop at the last file in the folder
for k = start:out
FM = FMdir(k).name;
InM = InMdir(k).name;
ProcessFilesOld(FM, InM, k) %FUNCTION
%more code
end
elseif v.Release=='(R2019b)'
for n= 1:FMn %cycle through files in the directory
finalname = FMdir(n).name;
end
start = 1; %start at one
out = n; %stop at the last file in the folder
for i = start:out
num_strfinal = num2str(i, '%02.f'); %cast the number as a string (make the format be 0#)
number = string(num_strfinal);
FM = dir(fullfile(filepath,'Processfiles\*FM')+ number + '_*.ep'); %Pull out the final measurement file being used
FinalM = getfield(FM, 'name'); %pull out and save the name field
IN = dir(fullfile(filepath,'Processfiles\*InM')+ number + '_*.ep'); %Pull out the initial measurment file being used
InitialM = getfield(IN, 'name'); %pull out and save the name field
ProcessFilesNew(FinalM, InitialM) %FUNCTION
%
% More code
end
end
Example of ProcessFilesNew code
function [Final,Initial,Difference,serial ] = ProcessFilesNew(FinalM, InitialM )
opts = detectImportOptions(FinalM, 'FileType','text'); %Set the file type as a text
opts.DataLines = [1, Inf]; %Start at one and go through the entire document, regardless of length
opts.Delimiter = ','; %look for 2 column data
opts.VariableNames = ['Frequency', 'Magnitude']; %Label x-axis and y-axis
opts.VariableTypes = ["double", "double"]; %Import data as doubles
opts.ImportErrorRule = "omitrow"; %If data isn't double then ignore row
opts.MissingRule = "omitrow"; %If data is missing, ignore row
opts.ExtraColumnsRule = "ignore"; %Ignore extra columns
opts.EmptyLineRule = "read";
Final = readtable(FinalM, opts);
%% REPEAT THIS CODE BUT FOR InitialM
If you need more code let me know, I figured you wouldn't appreciate sifting through 140 lines if you didn't need it.
Please help me figure out how to make the functions work, thank you
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 1 月 15 日
FM = dir(fullfile(filepath,'Processfiles\*FM')+ number + '_*.ep'); %Pull out the final measurement file being used
FinalM = getfield(FM, 'name'); %pull out and save the name field
The code is going to have problems if there is more than one _*.ep file
dpb
dpb 2021 年 1 月 16 日
" I have no idea how to properly execute the function in the main program. "
What does this mean? Just have the toplevel be either a script or a function that may need no inputs and kick it off...what else is needed; the version conditional logic is inside it.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2021 年 1 月 16 日
編集済み: Jan 2021 年 1 月 16 日
This:
for n = 1:FMn %cycle through files in the directory
finalname = FMdir(n).name;
end
does the same as:
finalname = FMdir(FMn).name;
The shown function ProcessFilesNew cannot run in R2012b, because Matlab handles the string type since R2016b.
Note that:
['Frequency', 'Magnitude']
is the same as:
'FrequencyMagnitude'
a concatenated CHAR vector.
["double", "double"]
is something completely different: a vector of 2 strings.

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by