put a function inside loop and save as matrix and csv
이전 댓글 표시
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
2019년 3월 18일
(Answers Dev) Restored edit
채택된 답변
추가 답변 (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
"Eval isn't exactly efficient"
"but it's OK with you want the files separate."
It is not "OK": it is slow, buggy, obfuscated, and complex. In fact the MATLAB documentation specifically advises against doing this: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array." Beginners who use eval force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug.
All MATLAB experts will advise against doing this. Read more about why here:
joms
2018년 7월 25일
Krithika A
2018년 7월 25일
Haha a bit touchy about the eval function?
But thank you, I didn't know that about eval. Will try to avoid for future.
joms
2018년 7월 25일
Krithika A
2018년 7월 25일
So the way I save into a matrix is using this:
files = dir('*.txt'); % You would use .csv
x_raw = [];
for q = 1:length(files);
data = importfileB(files(q).name); % I made importfileB function myself
x_raw = [x_raw, data]; % Puts data into a matrix
end
You'll need to adjust this yourself to fit your data structure. If it works, that is.
joms
2018년 7월 25일
"Haha a bit touchy about the eval function?"
Not really. I just read the documentation. Using eval forces beginners into writing slow, buggy, complex code. Why some beginners want to do this is a mystery to me, especially as much more efficient code is simpler to write!
Krithika A
2018년 7월 25일
To John: You might find this code and this link useful for specific directories:
load(fullfile('..', 'folderName1', 'folderName2', 'filename.ext'))
https://uk.mathworks.com/help/matlab/matlab_env/specify-file-names.html
To Stephen: Well, they are beginners. That might give you a hint. It is useful to know about eval being slow. So far, I've only used really small sets of data with minimum processing so eval has been OK so far.
"Well, they are beginners."
Exactly. Which is why it is the right time to learn how to write good code.
"That might give you a hint."
A hint to what? I don't know what your "hint" is, and I cannot guess. Please tell me.
"It is useful to know about eval being slow"
Yes, it is. And it is also useful to know that dynamically accessing variable names makes code slow, complex, increases the risk of bugs, and makes code harder to debug. That is why the MATLAB documentation specifically recommends avoiding the code you gave in your answer.
"So far, I've only used really small sets of data with minimum processing so eval has been OK so far."
??? I don't see any reason why "beginners" means you have to teach them slow, complex, buggy code that is harder to debug. The simpler code in my answer is more efficient and easier to learn. Even beginners are allowed to read the documentation and learn how to write efficient code. It is actually easier!
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.
"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).
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!