Reading csv from the second line and creating output

I want from a csv archive to read only one column. The problem is that I want to read this column from the second line and by using these commands:
[d1,tex]= xlsread(filename1);
name=tex(:,4)
it's reading from the first line.
Also, I would like to create a matrix that will inclue two columns that have come from commants (equations etc) in my Matlab code.

 Accepted Answer

Try this:
name = tex(2:end,4)
Also, I would like to create a matrix that will inclue two columns that have come from commants (equations etc) in my Matlab code.
Please provide details.

15 Comments

Ravnalis Michael’s Answer moved here:
Look, I would like to make a matrix which will include 2 columns . The first column will include the "name" and the other one will include onether result, for example name2=d1(:,7); (d1 is an input archive). In conlusion, one matrix with the results
Create a cell array or a table, depending on what you want to do.
Example (cell array):
M = {tex(2:end,4), d1(2:end,7)}
See the documentation on Cell Arrays for details.
The name sounds like character, but the other value might be numeric. You would need to use a cell array or a table() object.
You should probably be considering readtable()
Ravnalis Michael’s Answer moved here:
ok , it works but i have one last problem about the export of my results . I want to export the matrix "M = {tex(2:end,4), d1(2:end,7)}" into a txt. I am using these commants:
fid = fopen('final.txt','wt');
fprintf(fid,'%.6f\n',M);
fclose(fid);
but it not works...
Do you have any idea why ?
It is most likely not working because you are attempting to write a cell array containing character or string variables and numerical variables using fprintf.
If you have R2013b or later, use the cell2table function, then the writetable function.
Example:
TM = cell2table(M);
writetable(TM, 'final.txt');
I have not tested this, so I am listing it as UNTESTED CODE. It should work.
What datatype is tex? Is it cell array of character vectors, or is it string() objects?
@Ravnalis Michael — Are you still having problems? If so, what is wrong?
@Star Strider the problem is that in the 'final.txt' archive has not the format that i want. Basically the format consist of two line, where first row has this format:
M1,M2_ 1,M2_ 2,M2_ 3,M2_ 4,M2_ .........
and the second line has this format:
1, 2, 3.
I would like to have vertical format and in the first column to see the full name of the cells, and not M1,M2_ 1,M2_ 2,M2_ 3,M2_ 4,M2_ .........
M = [tex(2:end,1), num2cell(d1(2:end,7))].'; %transpose important
fprintf(fid,'%15s %.6f\n',M{:} );
Adjust the width of the string as needed.
@Walter Roberson Thank a lot, now it works exactly as I want !!!
You most likely have to transpose your data to column vectors before you put them in your cell array (or table).
I have no ideq what you are doing with your code. I cannot determine what the problem is and fix it without your data and a description of what you want.
This example code does what you say you want:
M = {'abc' 2; 'def', 3; 'ghi' 42};
TM = cell2table(M);
producing:
TM =
3×2 table
M1 M2
_____ __
'abc' 2
'def' 3
'ghi' 42
This version of my code:
names = {'abc', 'def', 'ghi'};
d1 = [1, 2, 42];
TM = table(names', d1')
(note the transpose operator (')) produces the same result:
TM =
3×2 table
Var1 Var2
_____ ____
'abc' 1
'def' 2
'ghi' 42
You may have to use the table function rather than cell2table. I have no idea because I can’t see your data or your code.
Did the approach in my previous Comment work, or are you still having problems?
Star Strider everything it's ok! Thank you for helping me !!
My pleasure!
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!