how do I use importdata() to import specific columns of data from a .txt file?
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
The file is called bubbles.txt and has been attached to this question. I'm trying to import the hours accessed and cumulative bubbles columns, however, I am not completely sure how to proceed. So far my code looks like this:
clear all; close all; clc
M = importdata('bubbles.txt', ' ', 1);
for k = [1, 3]
disp(M.colheaders(1, k))
disp(M.data(:, k))
disp(' ')
end
This returns an error saying:
Dot indexing is not supported for variables of this type.
Error in lab8t2 (line 9)
disp(M.colheaders(1, k))
How can I solve this issue?
Regards
1 Kommentar
Stephen23
am 13 Sep. 2023
" How can I solve this issue?"
Avoid fragile IMPORTDATA. Use READTABLE instead.
Antworten (1)
dpb
am 13 Sep. 2023
Bearbeitet: dpb
am 13 Sep. 2023
importdata returns an array, not a struct nor table -- it has its uses, but I generally avoid it for more specific functions that do what really want. There's no real reason to not just read the file instead of getting too fancy; here a table would seem appropriate--
tBubbles=readtable('bubbles.txt');
head(tBubbles)
and you've got everything...and now dot indexing will return the respective data you want directly...
plot(tBubbles.HoursElapsed,tBubbles.CumulativeBubbles)
xlabel('Hours'), ylabel('Number Bubbles (Total)')
for example...
2 Kommentare
Walter Roberson
am 14 Sep. 2023
importdata() can return any of three different datatypes, depending on what data it finds in the file. I recommend against using it, recommending that instead you use a function such as readtable() that returns predictable datatype.
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
