function arguments block for replacing data in existing excel file

Hi,
I am new to function arguments block. I am trying to use it to replace data in an existing excel file data.csv by doing some calculations that use input data from another csv file InputData.csv. Is this the correct format I should use to do this?
function f = replacedata(InputData.csv)
arguments
InputData.csv (1,1) string {mustBeFile(InputData.csv)}
end
% place calculations that use data from InputData.csv file here.
end

6 件のコメント

CAM
CAM 2023 年 3 月 24 日
The basic syntax looks OK to me.
But why is your input argument "InputData.csv"? You can't have a period in variable name, unless it is a structure. If InputData.csv is an actual file name, then it needs to be in quotes.
Davindra Usov
Davindra Usov 2023 年 3 月 26 日
thanks!
Walter Roberson
Walter Roberson 2023 年 3 月 26 日
function f = replacedata(InputData)
arguments
InputData (1,1) string {mustBeFile(InputData)}
end
% place calculations that use data from InputData file here.
end
Davindra Usov
Davindra Usov 2023 年 3 月 28 日
thank you
Davindra Usov
Davindra Usov 2023 年 3 月 29 日
is there a way to read in opt variables from this function in another function? data = namedargs2cell(opts)
read(data) does not work
Jack
Jack 2023 年 3 月 29 日
The format you provided for the function definition is not correct.

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

回答 (1 件)

Jack
Jack 2023 年 3 月 29 日
The format you provided for the function definition is not correct. Here's a corrected version:
function f = replacedata(input_csv)
% input_csv: file path to InputData.csv
arguments
input_csv (1,1) string {mustBeFile(input_csv)}
end
% Read data from input_csv file
input_data = readmatrix(input_csv);
% Place calculations that use data from InputData.csv file here.
% ...
% Write results to data.csv file
writematrix(results, 'data.csv');
end
In this code, input_csv is the file path to the InputData.csv file. The arguments block is used to validate that the input_csv argument is a string representing a valid file path. You can then use readmatrix to read the data from the InputData.csv file and perform your calculations. Finally, you can use writematrix to write the results to the data.csv file.

製品

リリース

R2021b

質問済み:

2023 年 3 月 23 日

回答済み:

2023 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by