read matrix from txt. files

17 ビュー (過去 30 日間)
polo Mahmoud
polo Mahmoud 2019 年 10 月 30 日
コメント済み: polo Mahmoud 2019 年 11 月 5 日
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 件のコメント
polo Mahmoud
polo Mahmoud 2019 年 10 月 31 日
編集済み: polo Mahmoud 2019 年 10 月 31 日
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.
Met V
Met V 2019 年 10 月 31 日
importdata('filename.txt')

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 31 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 5 日
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.
polo Mahmoud
polo Mahmoud 2019 年 11 月 5 日
Thank you very much :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by