how do i read just the header in a csv file and write them into a file

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

One approach —
header='field1,field2,field3,field4,field5'
header = 'field1,field2,field3,field4,field5'
headerstring = string(strsplit(header,',')).'
headerstring = 5×1 string array
"field1" "field2" "field3" "field4" "field5"
.

7 Commenti

Star Strider that worked perfectly, thank you!
i'm still having trouble figuring out how to combine fid and fopen and fprintf to write the headerstring into a newfile. maybe there's a better way than that to write the output to a text file?
My pleasure!
That is likely not necessary. Just use writetable or writematrix with your data. Those functions take care of all the details.
NOTE — The string variable type can be concatenated with numeric variables in an array —
header='field1,field2,field3,field4,field5'
header = 'field1,field2,field3,field4,field5'
headerstring = string(strsplit(header,',')).'
headerstring = 5×1 string array
"field1" "field2" "field3" "field4" "field5"
newData = [headerstring rand(5,3)]
newData = 5×4 string array
"field1" "0.78641" "0.723" "0.072686" "field2" "0.76182" "0.21978" "0.99947" "field3" "0.10367" "0.84176" "0.35115" "field4" "0.50745" "0.30978" "0.19069" "field5" "0.036857" "0.18852" "0.4987"
% writetable(newData,'yourFileName.csv') % Write The Data To 'yourFileName.csv’
The file extension (defining the file type) can be whatever you want (within limits) such as .txt , .xlsx or whatever else you want to save it as. All these functions and options are available in R2021b. (I commented that line because I do not want to write the file here.)
EDIT — (5 Jul 2022 at 17:50)
I also suggest using readmatrix or readtable to import the file. They make everything much easier.
.
i figured it out using the function writematrix
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
i don't see where to click on accept it
well now i've got two txt files with the string data in one column, i need to compare the fields within the two files to find out which fields are contained in both files, like this:
file1.txt has
field1
field2
field3
field4
field5
file2.txt has
field1
apples
bannans
field5
oranges
i should get a new file that has
newfile.txt
field1
field5
Thank you!
The ismember function is appropriate here —
file1 = ["field1"
"field2"
"field3"
"field4"
"field5"];
file2 = ["field1"
"apples"
"bannans"
"field5"
"oranges"];
Lv = ismember(file1, file2) % Logical Vector
Lv = 5×1 logical array
1 0 0 0 1
Result = file1(Lv)
Result = 2×1 string array
"field1" "field5"
... as desired!
You can also use ‘Lv’ here as the row (first) index to refer to multiple columns of the matrix, something like:
newDataExtract = newData(Lv,:)
if necessary.
.

Accedi per commentare.

Più risposte (2)

when the string arrays are of the same length, that works really well
but if the string arrays are not the same length (and in general my string arrays are not) then things get tricky, it appears the
Lv = ismember(file1, file2)
still gives me the correct answer, giving zeros for the additional rows if the length of file1 is greater than file2
and in this case Result=file1(Lv) gives the right answer
Also, a string that exists in both files may not be in the same row in both files, again
Lv = ismember(file1, file2)
seems to find them both but then the question of which but Lv = ismember(file2, file1) gives back a differnt answer, and so does the Result commands
Result=file1(Lv) or Result=file2(Lv)
so the question is which do I assign to which? do i always assign the file1 equal to the file with the greater length?
i kind of think that's what i should do, and then also use Result=file1(Lv) i've played with it and i can't seem to find the right combination that works for all cases of different array lengths and fields existing in both files but not in the same order:
Lv = ismember(file1, file2) with Result=file1(Lv)
or
Lv = ismember(file1, file2) with Result=file2(Lv)
or
Lv = ismember(file2, file1) with Result=file1(Lv)
or
Lv = ismember(file2, file1) with Result=file2(Lv)

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)
A = 5×1
1 6 4 1 6
B = randi(9, 7, 1)
B = 7×1
2 4 3 6 8 9 6
[C,ia,ib] = intersect(A,B)
C = 2×1
4 6
ia = 2×1
3 2
ib = 2×1
2 4
Aia = A(ia)
Aia = 2×1
4 6
Bib = B(ib)
Bib = 2×1
4 6
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.
.

Accedi per commentare.

Adam Jurhs
Adam Jurhs il 7 Lug 2022
Modificato: Adam Jurhs il 7 Lug 2022
thanks so much for your help!!!
i don't understand the necessity of ia and ib maybe i dob't need them at all. i think they are the indices of the array where the two overlapping values occur, right? i think in my situation (after reading the help on intersect ) i would use C=intersect(A,B) 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?
thanks again for your help
Todd

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)
Lia = 1×4 logical array
0 0 1 1
A(Lia)
ans = 1×2
4 2
Lia = ismember(B,A)
Lia = 1×6 logical array
1 1 1 1 0 0
B(Lia)
ans = 1×4
2 4 4 4
C = intersect(A,B)
C = 1×2
2 4
C = intersect(B,A)
C = 1×2
2 4
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.
.
gottcha
ismember worked perfectly on the "real" files. the only trick (and i've got it figured out, i just have to be careful) is getting the files' headers into string arrays.
hey thanks again for all your help!!!
how do i close out this thread saying i'm really happy with the answers?
Todd
As always, my pleasure!
You just did! (Also by accepting my answer, for which I thank you!)

Accedi per commentare.

Categorie

Prodotti

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by