how to import a text file in to matlab

4 ビュー (過去 30 日間)
jon
jon 2018 年 3 月 9 日
回答済み: Rik 2018 年 3 月 9 日
Hi all, just a quick question, how do I import a text file I have into matlab? Currently when I am writing my code I keep getting an error telling me Undefined variable "test1" or class "test1.txt".
filename = 'test1.txt';
delimiterIn = ',';
A = importdata(filename,delimiterIn);
fileID = fopen('test1.txt', 'r');
formatSpec = '%d %d %d %d';
sizeA = [4 Inf];
A = fscanf(test1.txt,formatSpec,Inf)
fclose(test1.txt);
A = A'
and I'm not too sure what is the problem with my code. Im really new to matlab so I would appreciate your help. Thank you!

採用された回答

Rik
Rik 2018 年 3 月 9 日
You should use the fileID variable, as that is the handle to your file. You didn't define test1.txt as a variable of class, so Matlab doesn't know what you mean. So assuming no other bugs, this code should work:
filename = 'test1.txt';
delimiterIn = ',';
A = importdata(filename,delimiterIn);
fileID = fopen('test1.txt', 'r');
formatSpec = '%d %d %d %d';
sizeA = [4 Inf];
A = fscanf(fileID,formatSpec,Inf)
fclose(fileID);
A = A'

その他の回答 (0 件)

カテゴリ

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