manipulate matrix dimension (ignoring lines)

I have number of matrices sized (203*12),however Matlab doesn't read the first two lines because they are texts. Therefore, I want command to ignore these two lines for all the number of matrices resulting new matrices (201*12) in dimension.
thanks

 Accepted Answer

Use the textscan (link) function.
Define: 'HeaderLines',2 in the name-value pair arguments.

26 Comments

Thank you Star Strider, the aim is to ignore these two lines not to read it
As always, my pleasure.
The 'HeaderLines',2 definition will skip the lines.
oh great, I used load(strcat) inside for loop, where i should apply 'Headerlines',2, or i should use textscan instead
To the best of my knowledge, the 'HeaderLines',2 name-value pair argument only works with textscan.
If your numeric matrix has 12 columns, something like this will work:
fidi = fopen( ... ); % Insert The Name Of Your File
FormatString = repmat('%f', 1, 12); % Format For Reading Numeric Data
data = textscan(fidi, FormatString, 'HeaderLines',2); % Read File
You may need to add other name-value pair arguments, depending on the requirements of your file. See the documentation on textscan for details.
sounds great, I will test and back to you Star Strider,
Thank you very much
As always, my pleasure.
Najiya Omar
Najiya Omar on 13 Jan 2018
Edited: Najiya Omar on 13 Jan 2018
dosen't work!! This my script
I have no idea what you are doing. The code you posted makes no sense.
See the documentation for textscan that I linked to in my original Answer.
Star is correct that your code makes no sense. You need to read some of the Matlab 'Getting Started' documentation.
I think I can see what you are trying to do, though.
Hopefully a few hints will help.
  1. I'm surprised you didn't get an 'Unexpected MATLAB expression' error when you defined nnew. A leading single quote does NOT create a string (char vector) like it does in Excel.
  2. The load command cannot be used to load text data files. Star's suggestion to use textscan is a good one.
  3. I suggest eliminating nnew entirely and changing your loop to for i=1:4. Then create the filename similarly to your strcat command (the part inside your load) but replacing nnew(i,:) with num2str(i). Actually, if I were doing it, I would use sprintf, but strcat should work. For example: filename = strcat(...
  4. Then, use Star's suggestion and use fopen and textscan (with an appropriate format spec) to read the files. Since you are doing this in a loop - reading multiple files - you will want to save the data for each file separately (I suggest using a cell array so replace data = ... with data{i} = ... in your loop). Or, you could put your plot command inside the loop (and use hold on) so you get each set of data on the same plot.
  5. Be sure to fclose your file at the bottom of the loop.
  6. Number one suggestion: read the documentation for all of the commands that Star and I have suggested. There are examples in there that will really help you understand how things are working. Also, read the documentation on using the debugger. You can step through your code and see where things are going wrong.
Good luck
Najiya Omar
Najiya Omar on 13 Jan 2018
Edited: Najiya Omar on 13 Jan 2018
Hello Star and Les Beckham,
Thank you very much for your suggestions. I would like to explain my idea briefly. I recored one word for two speakers.each speaker repeated this word 10 times based on different recording time duration from 0.5 second To 5. For each time duration like 0.5s will measure different features. My aim to plot all cases of recording for speaker one with one feature and I have 5 features. So, in total, it will have 5 plots for each speakers. the lines that i want to skip are in features matrices.
‘... the lines that i want to skip are in features matrices.
We have to know more about the file structure. We cannot guarantee that we can recover the information you want from the files that you saved.
@Najiya Omar: Please use flags only to inform admins and editors about postings, which might conflict with the terms of use.
If you want to say thank you, use a comment. Thanks.
Najiya Omar
Najiya Omar on 13 Jan 2018
Edited: Najiya Omar on 13 Jan 2018
the matrices saved as file.dat, the two first lines are text. the size of matrix is (201*12) in dimension, after skipping the text lines will generate (199*12)
The prototype code in my previous Comment (link) should work.
If it does not, attach one of your files to your original post or to a Comment so we can find out what the problem is.
I am not able to attached file.dat because this what i got Cannot attach this file because:
File format is unsupported. Use any of these formats: .bmp, .csv, .fig, .gif, .jpg, .jpeg, .m, .mlx, .mat, .mdl, .pdf, .png, .txt, .xls, .zip
So all you have to do is to change the file extension to ".txt" or zip the file.
@ Jan Simon, Sure i will, thank you for letting me know.
Your text file ‘LPCC.m’ (that should be named ‘LPCC.txt’) can be read with:
fidi = fopen('LPCC.m', 'rt');
D = textscan(fidi, repmat('%f', 1, 13), 'HeaderLines',2, 'CollectOutput',1);
or more appropriately:
fidi = fopen('LPCC.txt', 'rt');
D = textscan(fidi, repmat('%f', 1, 13), 'HeaderLines',2, 'CollectOutput',1);
Note that there are 13 columns — not 12 — in each line.
Najiya Omar
Najiya Omar on 13 Jan 2018
Edited: Najiya Omar on 13 Jan 2018
works great!!! Please accept my deepest thanks and if you don't mind might have another questions after test all files.
As always, my pleasure!
I will do my best to address your other questions.
Hello star, how can I do his command in loop under different folders in different directories.
if true
data = load(strcat('C.....', filePaths(i,:) ,LPCC.dat')); end
Even does not work
Then write your own special, custom reader for the file.

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!