I have file .txt contains
1 2
2 3
I want to read the file and put each value 12 , 23 in a cell array
How can I do that
Please help me.
Thanks

6 Kommentare

dpb
dpb am 9 Dez. 2019
Are the inputs actually separate in the file as shown and you really want them combined as numeric?
Why use a cell array for numeric values?
Mira le
Mira le am 9 Dez. 2019
I work with cell array the output like that
SI { 1 2 , 2 3}
SI{1}
1 2
SI{2}
2 3
Guillaume
Guillaume am 9 Dez. 2019
You haven't answered dpb question. The simplest way to clarify what your input file actually looks like is to attach an example.
Also, please explain why you want the result as a cell array when using a matrix would make everything simpler.
Mira le
Mira le am 9 Dez. 2019
SO how do we make it with matrix?
Adam Danz
Adam Danz am 9 Dez. 2019
@Mira le , please take a moment to think about the questions that dpb and Guillaume have asked. They are important.
Do you want this
{12, 23}
or this
{[1,2],[2,3]}
or something else?
If the answer is something else, show us exactly what you're looking for.
Mira le
Mira le am 9 Dez. 2019
I mean {[1,2],[2,3]}
If i want to put then in s for example like that
s={[1,2],[2,3]}
s =
1×2 cell array
[1×2 double] [1×2 double]
>> s{1}
ans =
1 2
>> s{2}
ans =
2 3

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Adam Danz
Adam Danz am 9 Dez. 2019
Bearbeitet: Adam Danz am 9 Dez. 2019

0 Stimmen

Assuming your text file is named myTextFile.txt and contains only that matrix,
A = readmatrix('myTextFile.txt'); % for Matlab >= 2019a
%A = dlmread('myTextFile.txt'); % for matlab < r2019a
B= mat2cell(A,[1,1],2).'; % If you want to split the matrix by rows
B= mat2cell(A.',[1,1],2).'; % If you want to split the matrix by columns
In your example it's unclear whether your splitting the matrix by rows or by columns since both result in the same output. Use the 2nd line above to split by rows and the 3rd line to split by columns. Both produce a 1x2 cell containing two 1x2 vectors.
[update]
This version below is more generalizeable in case the size of your matrix changes.
B = mat2cell(A,ones(size(A,1),1),size(A,2)).'; %by row....
B = mat2cell(A.',ones(size(A,2),1),size(A,1)).'; %by column...

6 Kommentare

Mira le
Mira le am 9 Dez. 2019
Undefined function or variable 'readmatrix'.
Adam Danz
Adam Danz am 9 Dez. 2019
When you create a question in this forum, it's important to fill in the version of Matlab you're using. readmatrix() was released in r2019a. Apparently you're using an earlier version.
Use this line instead.
A = dlmread('myTextFile.txt')
Mira le
Mira le am 9 Dez. 2019
I use 2017a version matlab
Adam Danz
Adam Danz am 9 Dez. 2019
dlmread should work for you, then. I'll add your release number to the question details on the right.
Mira le
Mira le am 10 Dez. 2019
Thanks
Amir Shahang
Amir Shahang am 4 Dez. 2021

That works for me too, thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2017a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by