put a function inside loop and save as matrix and csv
2 views (last 30 days)
Show older comments
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
Accepted Answer
Stephen23
on 25 Jul 2018
Edited: Stephen23
on 25 Jul 2018
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
5 Comments
Stephen23
on 22 Oct 2019
Edited: Stephen23
on 22 Oct 2019
@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
on 23 Oct 2019
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).
More Answers (1)
Krithika A
on 25 Jul 2018
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 Comments
Krithika A
on 25 Jul 2018
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
on 26 Jul 2018
Edited: Stephen23
on 26 Jul 2018
"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).
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!