how to use the str2num for the whole array at once
Show older comments
Dear all, when I read a text file and import it as an array A, sometimes I use the function str2num to return the imported vectors into a numerical form, but I need to do it for each vector alone. is there anyway please to reshape the whole array A into a numeric matrix at one time? like can i use the str2num to the whole matrix A and call it B for example?
thank you
13 Comments
per isakson
on 1 Nov 2017
Edited: per isakson
on 1 Nov 2017
- "array A" is that a cell array?
- Could you provide a tiny example?
- Why import a text file with numerical data into character data and convert to numerical in a second step?
MAHMOUD ALZIOUD
on 1 Nov 2017
Stephen23
on 1 Nov 2017
@MAHMOUD ALZIOUD: why do you use char? What is the file format? Is the file data string, numeric, or mixed?
It would be most useful if you uploaded a sample file for us to work with: add a comment and click the paperclip button. The sample file does not need to be the full file, just enough to be representative of the file format.
MAHMOUD ALZIOUD
on 1 Nov 2017
@MAHMOUD ALZIOUD: please make a new comment and upload a sample file by clicking the paperclip button.
We cannot run code using a screenshot, and no one is going to copy all that data from your screenshot. Screenshots are useless for doing anything with.
MAHMOUD ALZIOUD
on 1 Nov 2017
MAHMOUD ALZIOUD
on 1 Nov 2017
per isakson
on 1 Nov 2017
Edited: per isakson
on 1 Nov 2017
"note that it must be 105 characters in the columns"
- The longest row of NewTextDocument.txt is 57 characters.
- Does that mean that the rows shall be padded with space?
- "Open = A(:,22:24)" those columns contains space
- "xxxxx_spacing" What kind of variables are those that contain "spacing" in their names?
Stephen23
on 1 Nov 2017
@MAHMOUD ALZIOUD: can you tell us something about the file format? Note that the answer to your original question would be to simply load all of that numeric data as numeric data. There is absolutely no point in loading it as strings and then converting to numeric.
MAHMOUD ALZIOUD
on 1 Nov 2017
Edited: per isakson
on 1 Nov 2017
per isakson
on 1 Nov 2017
Edited: per isakson
on 1 Nov 2017
- With releases older than R2017a (see Walters answer) you have to use an approach similar to your code.
- Which release do you use?
- The sizes of the intermediate variables are less than 100MB, thus the entire file may be read into memory. What's the size of your RAM?
MAHMOUD ALZIOUD
on 1 Nov 2017
MAHMOUD ALZIOUD
on 1 Nov 2017
Accepted Answer
More Answers (2)
Nicolas Schmit
on 1 Nov 2017
How about this?
A = {'1', '2', '3'}
B = cellfun(@str2num, A)
Walter Roberson
on 1 Nov 2017
0 votes
You have fixed width fields. You should see https://www.mathworks.com/help/matlab/ref/fixedwidthimportoptions.html if you have R2017a or later.
1 Comment
MAHMOUD ALZIOUD
on 1 Nov 2017
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!