You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
manipulate matrix dimension (ignoring lines)
2 views (last 30 days)
Show older comments
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
Star Strider
on 11 Jan 2018
Define: 'HeaderLines',2 in the name-value pair arguments.
26 Comments
Najiya Omar
on 12 Jan 2018
Thank you Star Strider, the aim is to ignore these two lines not to read it
Star Strider
on 12 Jan 2018
As always, my pleasure.
The 'HeaderLines',2 definition will skip the lines.
Najiya Omar
on 12 Jan 2018
oh great, I used load(strcat) inside for loop, where i should apply 'Headerlines',2, or i should use textscan instead
Star Strider
on 12 Jan 2018
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.
Najiya Omar
on 12 Jan 2018
sounds great, I will test and back to you Star Strider,
Thank you very much
Star Strider
on 12 Jan 2018
As always, my pleasure.
Najiya Omar
on 13 Jan 2018
Edited: Najiya Omar
on 13 Jan 2018
dosen't work!! This my script
Star Strider
on 13 Jan 2018
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.
Les Beckham
on 13 Jan 2018
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.
- 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.
- The load command cannot be used to load text data files. Star's suggestion to use textscan is a good one.
- 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(...
- 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.
- Be sure to fclose your file at the bottom of the loop.
- 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
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.
Star Strider
on 13 Jan 2018
‘... 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
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)
Star Strider
on 13 Jan 2018
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.
Najiya Omar
on 13 Jan 2018
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
Najiya Omar
on 13 Jan 2018
I sent file.m
Najiya Omar
on 13 Jan 2018
@ Jan Simon, Sure i will, thank you for letting me know.
Star Strider
on 13 Jan 2018
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
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.
Star Strider
on 13 Jan 2018
As always, my pleasure!
I will do my best to address your other questions.
Najiya Omar
on 15 Jan 2018
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
Najiya Omar
on 16 Jan 2018
Even does not work
Image Analyst
on 16 Jan 2018
Then write your own special, custom reader for the file.
More Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)