フィルターのクリア

Storing while loop values in a vector?? Error

1 回表示 (過去 30 日間)
Kayla Magee
Kayla Magee 2018 年 7 月 16 日
コメント済み: Magdy Saleh 2018 年 7 月 16 日
fid = fopen('example.txt');
sample=[]; %empty matrix count=1;
tline=fgetl(fid); while ischar(tline) disp(tline) sample(count)=tline; count=count+1; tline=fgetl(fid); end
fclose(fid);
I am trying to run this while loop and save each value consecutively in the vector "sample". "sample" should end with one column and an unknown amount of rows of strings.
I am getting the error 'Subscripted assignment dimension mismatch.' Is it because I'm starting with an empty vector?? Is it because each row is a string?? Please help.

採用された回答

Magdy Saleh
Magdy Saleh 2018 年 7 月 16 日
It might be easier here to use a cell array as such
fid = fopen('example.txt');
sample={}; %empty matrix count=1;
j = 1
tline=fgetl(fid);
while ischar(tline)
disp(tline);
sample{j} = tline
tline=fgetl(fid);
j = j+1
end
fclose(fid);
  1 件のコメント
Magdy Saleh
Magdy Saleh 2018 年 7 月 16 日
This is because you are trying to save a matrix (2D vector), which requires each vector to have the same number of characters. A cell array, or a vector of vectors, gives you the freedom to have them with different lengths.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by