put a function inside loop and save as matrix and csv
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
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
5 个评论
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
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
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
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
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!