필터 지우기
필터 지우기

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

채택된 답변

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 CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by