Hi guys!
I have run into problems when I try to make a loop create variables and I am not very advanced in matlab. Here is what I am trying to do:
I have a .csv-file containing columns with data, each called test1, test2 and so forth. For each column, I am trying to create a variable in matlab - something like:
for i = 1:10
test(i) = csvvariable(:,i)
end
What I have done so far is manually creating a variable for each column in the .csv-file:
rfdtest = csvread('Hamstring.csv', 8, 1);
test1 = rfdtest(:,1);
if test1(1)>0
test1=[0;test1]
end
test2 = rfdtest(:,2);
if test2(1)>0
test2=[0;test2]
end
test3 = rfdtest(:,3);
if test3(1)>0
test3=[0;test3]
end
test4 = rfdtest(:,4);
if test4(1)>0
test4=[0;test4]
end
test5 = rfdtest(:,5);
if test5(1)>0
test5=[0;test5]
end
test6 = rfdtest(:,6);
if test6(1)>0
test6=[0;test6]
end
test7 = rfdtest(:,7);
if test7(1)>0
test7=[0;test7]
end
test8 = rfdtest(:,8);
if test8(1)>0
test8=[0;test8]
end
test9 = rfdtest(:,9);
if test9(1)>0
test9=[0;test9]
end
test10 = rfdtest(:,10);
if test10(1)>0
test10=[0;test10]
end
Later, I am going to do some calculations on each column, so this method will result in a lot of code.
Thanks in advance and feel free to ask for additional details!

2 comentarios

Stephen23
Stephen23 el 5 de Nov. de 2018
Editada: Stephen23 el 5 de Nov. de 2018
"I have run into problems when I try to make a loop create variables and I am not very advanced in matlab."
Magically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
The simplest alternative is to use indexing to access you data, which is neat, simple, easy to debug, and highly efficient (unlike what you are trying to do). All of the data are already trivially accessible using indexing from the variable csvvariable. Note that splitting up the data (pointlessly) duplicates it in memory.
"What I have done so far is manually creating a variable for each column in the .csv-file:"
Seems complex. Copy-and-pasting code is a sign that you are doing something wrong. Instead of copy-and-pasting code, get the computer to do it for you using loops and indexing. Copy-and-pasting causes bugs (e.g. your code repeating exactly the same test) and bloats the code.
Mikkel Eskildsen
Mikkel Eskildsen el 5 de Nov. de 2018
I was very aware my approach was definitely not the way to go, I just had to do it for myself to be able to gain an overview of what i'm trying to do.
I will look into indexing and see if I can figure out the way to do it, and otherwise come back for help. Thank you for your response!

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 5 de Nov. de 2018

1 voto

Unfortunately you've probably delayed any answer by not attaching 'Hamstring.csv', and not reading this link to learn how to format your code.
Also, read this
All of your "if" statements are the same
if test2(1)>0
but you have test3, test4, etc. so shouldn't you also change the if tests?

8 comentarios

Mikkel Eskildsen
Mikkel Eskildsen el 5 de Nov. de 2018
Sorry! I have attached the csv-file now and reformatted the code. Also, the if statements were an obvious typo, I forgot to change each to the corresponding test variable - thank you.
Image Analyst
Image Analyst el 5 de Nov. de 2018
You Accepted the answer, so did fixing the typos fix it? Is everything working now?
Mikkel Eskildsen
Mikkel Eskildsen el 6 de Nov. de 2018
Editada: Mikkel Eskildsen el 6 de Nov. de 2018
It sure did fix a mistake I made, but I didn't realize that you can only accept one answer until afterwards where I couldn't undo it. I still face my problem - which is now, how to efficiently access my data without having to manually create variables for each column and do my calculations on each one.
The other editor "Stephen Cobeldick" suggested using indexing to access my data, but after reading a few articles I think I need some suggestions on how I could proceed. Some additional info:
Each test-column in "Hamstring.csv" is data from Rate of Force Development Tests(RFD). My final product would be a GUI in matlab that reads the selected csv-file after browsing, and then a button, "Analyze", that performs a number of calculations on each test(column). The results is then shown to the user.
If you need more info do ask me, and I apologize for the confusion caused by not reading into posting questions beforehand!
Edit: I made the GUI already, so that is not not my issue. Also, I just found the unaccept button.
I might have figured it out by using cell arrays:
rfdtest = csvread('Hamstring.csv', 8, 1);
for i=1:10
T{i} = rfdtest(:,i);
end
Would this be simple and efficient to proceed with?
No. Cell arrays are not efficient. I don't know why you'd want or need a cell array. Isn't rfdtest a 2-D double matrix? Why can't you just deal with that? Or if you want to extract one column at a time and process it, then just do this:
for k = 1 : 10
thisColumn = rfdtest(:, k);
% Now do something with thisColumn. Probably no need to store it for use after the loop.
end
Mikkel Eskildsen
Mikkel Eskildsen el 7 de Nov. de 2018
I did it after reading the accepted answer in this topic. In the loop I am going to add a zero in the beginning of all columns where zero isn't the first element, which is why the columns will have different sizes. But if that it not going to have any influence, I will try using your example.
Mikkel Eskildsen
Mikkel Eskildsen el 7 de Nov. de 2018
I made it work with the simple loop you proposed. Thank you for your help!
You can do
for k = 1 : 10
thisColumn = rfdtest(:, k);
if thisColumn(1) ~= 0
thisColumn = [0; thisColumn];
end
% Now do something with thisColumn. Probably no need to store it for use after the loop.
end
Again, no inefficient cell arrays needed unless for some reason you need to keep/store all the individual columns after the loop ends, which you haven't shown any need for.
If you do, you'd to this:
ca = cell(10, 1); % Preallocate a cell array
for k = 1 : 10
thisColumn = rfdtest(:, k);
if thisColumn(1) ~= 0
thisColumn = [0; thisColumn];
end
ca{k} = thisColumn; % Store in a cell array.
% Now do something with thisColumn.
end

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 5 de Nov. de 2018

Comentada:

el 7 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by