how do i read just the header in a csv file and write them into a file
Mostra commenti meno recenti
hi guys,
i have a csv file that i'd like to get a listing of all the headers, transposed into one column, and then write them out to a new file. so far what i've done is:
str = fileread('filename.csv')
index = strfind(str, '1');
header = str(1:(index-1));
and that gives me a character array with a single a row of all the fields in header seperated by commas, see below
header='field1,field2,field3,field4,field5'
what i need is a new file with the field names written as a column
field1
field2
field3
field4
field5
thanks for whatever help you can give!
Todd
Risposta accettata
Più risposte (2)
Adam Jurhs
il 6 Lug 2022
0 voti
1 Commento
The ‘Lv’ vector indexes into the first argument of ismember. In the situation you describe, the first argument should be the longest vector, since that would correspond to the ‘Lv’ output.
I initially chose ismember because it appeared to be appropriate for the situation you describe. An alternative to experiment with, that may be closer to what you want, is the intersect function.
A = randi(9, 5, 1)
B = randi(9, 7, 1)
[C,ia,ib] = intersect(A,B)
Aia = A(ia)
Bib = B(ib)
The disadvantage of using intersect however is that it returns only the index of the first occurrence in each vector. If you expect only one match in each vector, that works. If there could be more than one match, and you want all of them, it won’t.
.
Adam Jurhs
il 7 Lug 2022
Modificato: Adam Jurhs
il 7 Lug 2022
0 voti
3 Commenti
As always, my pleasure!
‘i think they are the indices of the array where the two overlapping values occur, right?’
Right.
‘if that's the case, and knowing that there will only be one match within each array (in your example the number 6 would only occur once in both arrays A and B (they could just be located at different indices), i think the use of intersect and ismember will give the same result, yes?’
No. The functions behave differently. The longer vector should be the first argument to ismember, because the logical index result indexes into the first argument vector, however that does not matter with intersect. The advantage of using ismember is that it returns all the logical indices for the first argument that match the second argument, while intersect returns only the first instance. The function you use depends on the result you want.
Use the function that works best in your application. For intersect, use the output that indexes into the correct array, if necessary, otherwise just use the ‘C’ output, since it will give the values common to both vectors, regardless of their relative sizes.
To illustrate —
A = [5 3 4 2];
B = [2 4 4 4 6 8];
Lia = ismember(A,B)
A(Lia)
Lia = ismember(B,A)
B(Lia)
C = intersect(A,B)
C = intersect(B,A)
I demonstrated how intersect and its index vector outputs work earlier, so no need to repeat that here. It returns the indices of the first instance of each match only.
.
Adam Jurhs
il 7 Lug 2022
Modificato: Adam Jurhs
il 7 Lug 2022
Star Strider
il 7 Lug 2022
As always, my pleasure!
You just did! (Also by accepting my answer, for which I thank you!)
Categorie
Scopri di più su Cell Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!