How to get split to return a string array?

12 ビュー (過去 30 日間)
Katrina
Katrina 2020 年 12 月 9 日
コメント済み: Katrina 2020 年 12 月 10 日
Hello,
I am trying to read a text file and split each line into space-delineated components. Using split I always get a cell array as output, and then have to use cell2mat to convert each cell to a string. According to matlab documentation, if the input field is a string array, the output will be a string array, but that is not how it is working for me. What am I doing wrong?
str = fgetl(filepointer);
substrings = split(str);
Isn't substrings a string array? If I say ischar(substring), the answer is 1. I have also tried fgets but the result is the same.
It is just clunky to have to call cell2mat before processing every chunk of the string.
thank you.

採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 10 日
The low level file operations never return string objects.
tline = fgetl(fileID) returns the next line of the specified file, removing the newline characters.
  • If the file is nonempty, then fgetl returns tline as a character vector.
  • If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.
then have to use cell2mat to convert each cell to a string
No, that is not necessary.
You can proceed one of two ways:
substrings = string(split(str));
or
substrings = split(string(str));
I personally prefer the first of those two, but I can understand that some people might prefer the second of them.
  1 件のコメント
Katrina
Katrina 2020 年 12 月 10 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by