Dealing data with text and numerical in .txt file
Show older comments
I have a txt file containing some data as follows: how to visualize using Matlab? I request your suggestions.
"k: 0.1, omega_real: 20001, omega_imag: 11283.8
k: 0.30101, omega_real: 2208.33, omega_imag: 1245.35
k: 0.50202, omega_real: 794.574, omega_imag: 447.726
k: 0.70303, omega_real: 405.652, omega_imag: 228.301
k: 0.90404, omega_real: 245.711, omega_imag: 138.064
k: 1.10505, omega_real: 164.782, omega_imag: 92.404
k: 1.30606, omega_real: 118.247, omega_imag: 66.1498
k: 1.50707, omega_real: 89.0568, omega_imag: 49.6807
k: 1.70808, omega_real: 69.5509, omega_imag: 38.6757
k: 1.90909, omega_real: 55.8753, omega_imag: 30.9601
k: 2.1101, omega_real: 45.9183, omega_imag: 25.3424
k: 2.31111, omega_real: 38.4445, omega_imag: 21.1258
k: 2.51212, omega_real: 32.6919, omega_imag: 17.8803
k: 2.71313, omega_real: 28.1699, omega_imag: 15.329
k: 2.91414, omega_real: 24.551, omega_imag: 13.2872"
Answers (3)
%display the contents of the file
type data.txt
%read the file
fID = fopen('data.txt')
%read the data in the given format - as the data is read in column order by fscanf,
%provide the dimensions i.e. 3 columns and all the rows, and transpose the final output
data = fscanf(fID, 'k: %f, omega_real: %f, omega_imag: %f\n', [3 Inf]).'
2 Comments
Ismita
on 17 Feb 2024
Dyuman Joshi
on 17 Feb 2024
You're welcome!
If my answer solved your problem, please consider accepting the answer.
T = readtable('data.txt', 'Delimiter',{' ',':',','}, 'MultipleDelimsAsOne',true)
I am not certain what you intend by ‘visualize’.
Perhaps this —
T1 = readtable('M I T 2024 02 17.txt', 'Delimiter',{',',':'})
Lv = varfun(@(x)~isnumeric(x),T1);
Ln = find(table2array(Lv));
k = find(table2array(varfun(@(x)contains(x,'k'), T1(1,Ln))));
Re = find(table2array(varfun(@(x)contains(x,'real'), T1(1,Ln))));
Im = find(table2array(varfun(@(x)contains(x,'imag'), T1(1,Ln))));
k = table2array(T1(:,Ln(k)+1));
Real = table2array(T1(:,Ln(Re)+1));
Imag = table2array(T1(:,Ln(Im)+1));
figure
plot(k, Real, 'DisplayName','Real', 'LineWidth',2)
hold on
plot(k, Imag, 'DisplayName','Imag', 'LineWidth',2)
plot(k, abs(Real+1j*Imag), 'DisplayName','Abs', 'LineWidth',2)
hold off
grid
Ax = gca;
Ax.YScale = 'log';
xlabel('k')
ylabel('Magnitude')
legend('Location',' best')
.
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!