read matrix from txt. files

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 4
5 6 7 8]
% B =
[9 10 11 12
13 14 15 16]
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 out.

2 Comments

polo Mahmoud
polo Mahmoud on 31 Oct 2019
Edited: polo Mahmoud on 31 Oct 2019
i want some how to read where the start of this [ begins and all the numbers between until ] and then the same for the next matrix ?
Or maybe read all numbers from 1 head line to next head line and ect.
importdata('filename.txt')

Sign in to comment.

 Accepted Answer

S = fileread('YourFile.txt');
parts_text = regexp(S, '\[[\]*\]', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
parts_value will now (if all went well) be a cell array of numeric arrays, with one entry for each [] delimited block. In this code, it is not required that each block has the same number of rows or columns as the other blocks.

15 Comments

polo Mahmoud
polo Mahmoud on 31 Oct 2019
Edited: polo Mahmoud on 1 Nov 2019
i have attatched the text file, but i cant make it work with your code, can you help me with what i'm doing wrong
S = fileread('coord.txt');
parts_text = regexp(S, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
Thank you very much Walter Roberson :)
But can i ask you one last thing, what if the matrix contains letters in some of the rows, eg:
% A =
[ 1 2 3 IX
5 6 7 IX]
% B =
[ 1 2 3 E
5 6 7 E]
can it still find the letters too ?
Is the array a cell array or a string or character array? A cell array is the only data type where you can mix numbers and strings. A string array could hold them if the numbers were actually character strings (like the letters).
What does this say
whos A
whos B
Image Analyst, 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.
What do you want to have happen with the letters? Will they only appear at the end of the row?
i just want them to read, because they are defined in the scribe to a specifik number, but i want to be able to load a matrix contaning the these letters eg.
A = 20;
IX = 10;
and the matrix I load in from the txt file should be able to containig these letters and then, when I click run it should be able to transform the letters in the matrix to these above numbers
Do you have the Symbolic Toolbox?
yes like if the "user" writes this in the text file eg.
A = 30;
IX = 10;
% A =
[ 1 2 3 IX
5 6 7 IX]
% B =
[ 1 2 3 A
5 6 7 A]
and when you load this txt files in the matlab then i want the matlab to know what the matrix should look like:
% A =
[ 1 2 3 10
5 6 7 10]
% B =
[ 1 2 3 30
5 6 7 30]
Your coord2.txt file does not define the named variables it uses, so you need to define whether the return is to be:
  1. always a cell array of character vectors for every position, nothing interpreted as numeric; or
  2. always a cell array, in which all numeric-looking entries and all named variables that resolved to numeric are converted to numeric, and all unresolved character vectors are left as character vectors, but all in all every position is a cell; or
  3. like above, but with the additional step that if every position resolves to numeric then convert the entire array to numeric, so the code section can return either a numeric array (if everything resolved) or a cell array of mostly numeric but also character vectors if some did not resolve; or
  4. perhaps the section should error if given a file with unresolved variables
Your current setup appears to permit the user to define variables as arrays and use the variables in other variables. Is that a deliberate feature?
polo Mahmoud
polo Mahmoud on 4 Nov 2019
Edited: polo Mahmoud on 4 Nov 2019
yes i want the user to be able to define A and IX and then when imported/loaded into matlab it should only give me the matrix. because right now i have made a code that can read the matrix but when i try to define A and IX in the matrix it gives NaN in thoes places.
Can you show me a code or an example on how to be able to do that ?
named = regexp(S, '^\s*(?<varname>[A-Za-z]+)\s*=\s*(?<value>[^;]*)', 'lineanchors', 'names');
to_replace = cellfun(@(s) ['(?<=\W)' s '(?=\W)'], {replacements.varname}, 'UniformOutput', false);
replace_with = {named.value};
Smod = regexprep(S, {'^\s*[A-Za-z]+.*$', to_replace{:}}, {'', replace_with{:}}, 'lineanchors', 'dotexceptnewline');
parts_text = regexp(Smod, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
what should i write in {replacements.varname} and {named.value}
is it {XI} and {10} ?
named = regexp(S, '^\s*(?<varname>[A-Za-z]+)\s*=\s*(?<value>[^;\n]*)', 'lineanchors', 'names');
to_replace = cellfun(@(s) ['(?<=\W)' s '(?=\W)'], {named.varname}, 'UniformOutput', false);
replace_with = {named.value};
Smod = regexprep(S, {'^\s*[A-Za-z]+.*$', to_replace{:}}, {'', replace_with{:}}, 'lineanchors', 'dotexceptnewline');
parts_text = regexp(Smod, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
Sorry, I renamed the variable as I worked so I still had the old variable name in memory so it passed my testing...
{named.varname} and {named.value} are output from the regexp(), so you do not need to fill in anything.
The code finds all replacements of the form
name = value;
(with no % at the beginning of the line) in the file, and records them all first. Their position in the file is not taken into account. If any name is used twice, then the first of them will have effect.
The code then removes those defining lines, and substitutes the replacements textually without any attempt to understand them. If the line were
A = 10 20] 30 40;
then it would substitute '10 20] 30 40' for each occurance of 'A', whether or not it made sense to do so.
After the substitutions are done, it breaks up into parts according to the [ ] and then it runs textscan on each part to try to convert whatever is there into numeric.
Thank you very much :D

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!