put a function inside loop and save as matrix and csv

1 回表示 (過去 30 日間)
joms
joms 2018 年 7 月 25 日
コメント済み: Shaula Garibbo 2019 年 10 月 23 日
I have the following main script that pass value to a function. I would like to import different csv using function within a loop and output as summarize all data as matrix to be printed in an xls file . Here are my progress code so far, please help in matrix creationa and xls print. Thanks
%%main script-reads diff csv
clc
clear all
km=csvread('Bus_Data1.csv');
A=0
[Result1,maxa]=csvimport(km)
km=csvread('Bus_Data2.csv');
A=4
[Result1,maxa]=csvimport(km)
km=csvread('Bus_Data3.csv');
A=5
[Result1,maxa]=csvimport(km)
km=csvread('Bus_Data4.csv');
A=9
[Result1,maxa]=csvimport(km)
Here is the function code
%%function to calculate output of main m
function [Result1,maxa]=csvimport(fileName)
A=fileName(1:5,1);
maxa=max(A);
Result1=A;
end
  1 件のコメント
Rena Berman
Rena Berman 2019 年 3 月 18 日
(Answers Dev) Restored edit

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

採用された回答

Stephen23
Stephen23 2018 年 7 月 25 日
編集済み: Stephen23 2018 年 7 月 25 日
Simpler and much more efficient than ugly dynamic variable names:
S = dir('Bus_Data*.csv');
N = numel(S);
R = cell(1,N);
M = cell(1,N);
for k = 1:N
km = csvread(S(k).name);
[R{k},M{k}] = csvimport(km);
end
If you want the files loaded in numeric order then download my FEX submission natsortfiles.
  5 件のコメント
Stephen23
Stephen23 2019 年 10 月 22 日
編集済み: Stephen23 2019 年 10 月 22 日
@Shaula Garibbo : I guess that you are using FEX 23573 csvimport, in which case that error message occurs whenever the input is not a character array:
if ~ischar( fileName )
So you should check if the input really is a character vector (e.g. and NOT a string, if you are using MATLAB version >R2016b). Note that 'C:filepath' is not a valid path.
I strongly recommend that you use one of the inbuilt file importing functions:
Shaula Garibbo
Shaula Garibbo 2019 年 10 月 23 日
Thank you. I'll try a different approach as per your suggestion. (I know 'filepath' isn't valid, it's just that the filepath is ridiculously long so I thought I'd delete it to make the comment shorter - sorry, should have said).

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

その他の回答 (1 件)

Krithika A
Krithika A 2018 年 7 月 25 日
d = dir('Bus_Data*.csv'); % dir struct of all pertinent .csv files
n = length(d); % how many there were
for i = 1:n
data = csvread(d(i).name);
[Result1,maxa] = csvimport(data)
eval(['Result',num2str(i),'=Result1;'])
eval(['maxa',num2str(i),'=maxa;'])
end
Eval isn't exactly efficient, but it's OK with you want the files separate.
  11 件のコメント
Krithika A
Krithika A 2018 年 7 月 25 日
Ok... This is going nowhere. My point is that beginners make mistakes because they are beginners. I agreed entirely with everything else you said. That was it. Goodbye for now.
Stephen23
Stephen23 2018 年 7 月 26 日
編集済み: Stephen23 2018 年 7 月 26 日
"My point is that beginners make mistakes because they are beginners"
In reality all programmers make mistakes! Beginners make mistakes, experienced developers make mistakes, I make mistakes, you make mistakes, we all make mistakes. It is precisely because everyone makes mistakes that programmers have developed tools to help find mistakes in code, and to help fix them. These tools includes static code checking, which checks code in the MATLAB editor, before it is even run. These tools complete function names, show inputs and help, find syntax errors and suggest fixes for them:
But sadly some beginners like to use tools like eval, which mean that none of those code checking tools work. Those beginners force themselves into writing more complex code (the risk of bugs increases with code complexity) and at the same time they remove lots of useful tools that would actually help them to find and fix those bugs. This is one reason why experienced MATLAB users avoid eval, and instead write simpler, neater, less buggy, and easier-to-fix code. Your answer increases the risk of bugs and makes debugging harder: it is not clear to me how this is supposed to help those beginners who make mistakes (as you point out).

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by