make a matrix out of list

4 ビュー (過去 30 日間)
Nicle Davidson
Nicle Davidson 2021 年 10 月 16 日
編集済み: Nicle Davidson 2021 年 10 月 16 日
I have a file which has 4 words such as
mini
miny
milky
mouse
It could also be a file with more words, but one word per line.
I would like to after reading from the file, which might could be done with load('file.txt'); command,
make a matrix that is as long as I have words in this file.
so if I have 4 words, then the matrxi H shall be 4 rows long.
Is it a possibility?
  1 件のコメント
zahra hosaini
zahra hosaini 2021 年 10 月 16 日
hi
i think its possible
declare a parameter like i=1 (showing which row u are in)
make a loop to read all of your file and detecting the end of it as a break
make another loop inside of it for reading each line and detecting the end of it for break(i think it was parameter \0)
as u read the word in second loop put it in your matrix (use another parameter like j=1 for declaring your column and increase it each time you end the second loop)
increase i after getting out of the second loop to move to the next row
  • there will be a warning saying that "your matrix dimention is changing each time you run the file", but thats something that we want and its ok
  • also for your matrix column , u can set a fix number like 10(the longest world length that will be in your file)

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

採用された回答

DGM
DGM 2021 年 10 月 16 日
編集済み: DGM 2021 年 10 月 16 日
It's not really clear if you want the contents of the file to be read into an array, or whether you just want a numeric (or other unspecified) array with the same size.
Assuming the first case:
fid = fopen('fruitwords.txt','r');
A = textscan(fid,'%s');
fclose(fid);
A{:}
ans = 6×1 cell array
{'banana'} {'orange'} {'apple' } {'peach' } {'pear' } {'cherry'}
If it's the second case, the simple way would be to simply read the file as above and then use the size of A to allocate a new array.
mynewarray = zeros(size(A));
This may be expensive if the file contents are unneeded or cause memory burdens. Trying to accurately and efficiently get line counts might be a bit more complicated otherwise:
  1 件のコメント
Nicle Davidson
Nicle Davidson 2021 年 10 月 16 日
編集済み: Nicle Davidson 2021 年 10 月 16 日
Let's say I have a file as mentioned above.
Based on this file I have made a such matrix say H in another file:
Explaining what my matrix is: the numbers are ascii for letters, I want to register the positions of where I have capital letters, which have the asciies 65 to 90, in a new matrix say M.
I need to read the matrix H from the file it is stored in, and make a new matrix M which has this in content:
M=(4 5
5 7
5 10
2 12)
The numbers are the positions of capital letters in my H matrix. The point is also that the new matrix M will have same number of rows as I have letters in my word list. (in the example above it was 4) so M needs to have 4 words.
Sorry if I am not so good in explaining the problem. If it helps you can test on the mixAcs.mat file
How can I make a code for this

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 10 月 16 日
Read about importdata. You can import the data into MATLAB, the data will be a cell array.
S = importdata('myfile.txt') ;
celldisp(S)
  2 件のコメント
Nicle Davidson
Nicle Davidson 2021 年 10 月 16 日
Is S then a matrix, because doing this I can have an array. Is it possible to make a matrix which has as many rows as the list elements?
KSSV
KSSV 2021 年 10 月 16 日
S is a cell array, you can access the words by S{1},S{2}...S{n}.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by