Textscan, dlmread, and importdata all not working

I have a very simple text file comprised of 1's and 0's. I have tried to read it into MATLAB with every function I can find, but nonetheless I get an error of some form. dlmread seems the most promising, but it throws a "Mismatch between file and format character vector." error. textscan did not work as it created a 1X1 cell with all of the data in it, and trying A = A{:} also did not work, similar error with importdata. Any help would be much appreciated.

6 件のコメント

Adam Danz
Adam Danz 2018 年 6 月 29 日
編集済み: Adam Danz 2018 年 6 月 29 日
Could you attach the text file or a watered down version of the text file if it's huge? The line of code that calls dlmread() would be helpful, too.
Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
y = dlmread('FakeData.txt');
Star Strider
Star Strider 2018 年 6 月 29 日
‘... trying A = A{:} also did not work ...’
Try using the cell2mat function, or alternatively:
[A{:}]
Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
tried the cell2mat function, all of the data ends up in one cell for some reason so it returns a [] matrix.
Stephen23
Stephen23 2018 年 6 月 29 日
編集済み: Stephen23 2018 年 6 月 29 日
" textscan did not work as it created a 1X1 cell with all of the data in it"
Why do you think that having the data in one cell is a problem? What is important is how you called textcan and the content of the cell. But unless you give us this information we will just be guessing...
Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
Sorry, the goal is to delineate the data so that each value of 1 or 0 is parsed into its own cell. I plan on for looping through each individual value, and I don't think thats possible if its all in one cell (if it is that would be amazing).

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

 採用された回答

Adam Danz
Adam Danz 2018 年 6 月 29 日
編集済み: Adam Danz 2018 年 6 月 29 日

0 投票

See how this works for you,
fid = fopen('FakeData.txt');
t = textscan(fid, '%s');
fclose(fid)
t2 = cell2mat(cellfun(@str2double, t, 'uniformoutput', false));

8 件のコメント

Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
BANG! Thank you! I did not realize that the file was being read in as a string, so my formatspec operator was set to look for numbers. Thanks for the help
Paolo
Paolo 2018 年 6 月 29 日
Why not simply
t2 = str2double(t{:})
?
Adam Danz
Adam Danz 2018 年 6 月 29 日
Note that your first and last character in the FakeData text file has an additional strange character. When textscan reads this it replaces that strange character with a space. So you'll need to remove white space in 't' before converting it to double in 't2'. Otherwise you'll have NaN for those character.
Adam Danz
Adam Danz 2018 年 6 月 29 日
Yeah, that cell2mat() call can definitely be simplified as Star Strider and Paolo point out.
Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
I see that NaN error, but the text file has no such weird character, merely 1's and 0's -- see attached text file
Image Analyst
Image Analyst 2018 年 6 月 29 日
If I click on it in the Firefox browser, it shows that the first and next to the last character are weird - not a 0 nor a 1.
Adam Danz
Adam Danz 2018 年 6 月 29 日
Yeah, my downloaded txt file also has those weird chars.
Skyler Castillo-Wilson
Skyler Castillo-Wilson 2018 年 6 月 29 日
Update for those who helped: Naturally, the only issue was compatibility between what a number means to my PC keyboard vs. my MacBook Pro. That messed with the text file just enough to ruin all of those functions. Problem resolved!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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