matrix with characters and numbers from text file

Hi, if I have a txt. file with 2 different matrix in the text. file and i want them to be read into matlab, eg:
this is from the txt. file:
% A =
[ 1 2 3 E
5 6 7 E]
% B =
[9 10 11 IX
13 14 15 IX]
and i want matlab to know the diffrent between these two element when it reads the file. so i can make them longer or shorter and it should still be able to take both matrix our with the characters and numbers

4 Comments

Can you provide text file?
I have actually this as my code so far and the problem is the last matrix hope you can help me with this last matrix in the text file. this is the number of matrix i have and i can read almost all of them, but i can't with the last matrix, call 'connec' in the text file.
and my matlab code is this for now:
clear all;clc
S = fileread('coord2.txt');
parts_text = regexp(S, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
AAA = cell2mat(parts_value);
nCoord=AAA(:,1:4);
supp=AAA(:,5:10);
SF=AAA(:,11:16);
rho(:,1) = AAA(:,17);
E(:,1) = AAA(:,19);
But i cant read the last one because i think that it contains A and IX
It seems all matrices are extracted from the text file except last matrix
% connec
[1 2 1 2e11 A Ix
2 3 2 2e11 A Ix
3 4 3 2e11 A Ix
4 5 4 2e11 A Ix
5 6 5 2e11 A Ix];
It contained numerical with character data thats why it unable to perform
AAA = cell2mat(parts_value);
hence further statements you can't execute
In the cell variable parts_value you can get the all matrices correctly except last matrix as
parts_value{1}, parts_value{2}..parts_value{6}
so i cant get the last matrix out anyway Bhaskar R ?

Sign in to comment.

Answers (1)

You could use the import tool and even provide the url of the txt file (https://www.mathworks.com/matlabcentral/answers/uploaded_files/246014/coord2.txt)
You could then set the output type as a table/cell array/ whatever you like and then generate a script for it. You will notice that it eventually uses readtable with some options (not shown below)
% Import the data
coord21 = readtable("coord2.txt", opts);

1 Comment

it does not work for me, it says in the table NaN to all the "letters", but it can load the numbers.

Sign in to comment.

Categories

Products

Release

R2019a

Asked:

on 1 Nov 2019

Commented:

on 4 Nov 2019

Community Treasure Hunt

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

Start Hunting!