Dealing data with text and numerical in .txt file

2 次查看(过去 30 天)
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"

回答(3 个)

Dyuman Joshi
Dyuman Joshi 2024-2-17
%display the contents of the file
type data.txt
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
%read the file
fID = fopen('data.txt')
fID = 3
%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]).'
data = 15×3
1.0e+04 * 0.0000 2.0001 1.1284 0.0000 0.2208 0.1245 0.0001 0.0795 0.0448 0.0001 0.0406 0.0228 0.0001 0.0246 0.0138 0.0001 0.0165 0.0092 0.0001 0.0118 0.0066 0.0002 0.0089 0.0050 0.0002 0.0070 0.0039 0.0002 0.0056 0.0031

Stephen23
Stephen23 2024-2-17
T = readtable('data.txt', 'Delimiter',{' ',':',','}, 'MultipleDelimsAsOne',true)
T = 15×6 table
Var1 Var2 Var3 Var4 Var5 Var6 _____ _______ ______________ ______ ______________ ______ {'k'} 0.1 {'omega_real'} 20001 {'omega_imag'} 11284 {'k'} 0.30101 {'omega_real'} 2208.3 {'omega_imag'} 1245.3 {'k'} 0.50202 {'omega_real'} 794.57 {'omega_imag'} 447.73 {'k'} 0.70303 {'omega_real'} 405.65 {'omega_imag'} 228.3 {'k'} 0.90404 {'omega_real'} 245.71 {'omega_imag'} 138.06 {'k'} 1.1051 {'omega_real'} 164.78 {'omega_imag'} 92.404 {'k'} 1.3061 {'omega_real'} 118.25 {'omega_imag'} 66.15 {'k'} 1.5071 {'omega_real'} 89.057 {'omega_imag'} 49.681 {'k'} 1.7081 {'omega_real'} 69.551 {'omega_imag'} 38.676 {'k'} 1.9091 {'omega_real'} 55.875 {'omega_imag'} 30.96 {'k'} 2.1101 {'omega_real'} 45.918 {'omega_imag'} 25.342 {'k'} 2.3111 {'omega_real'} 38.444 {'omega_imag'} 21.126 {'k'} 2.5121 {'omega_real'} 32.692 {'omega_imag'} 17.88 {'k'} 2.7131 {'omega_real'} 28.17 {'omega_imag'} 15.329 {'k'} 2.9141 {'omega_real'} 24.551 {'omega_imag'} 13.287

Star Strider
Star Strider 2024-2-17
I am not certain what you intend by ‘visualize’.
Perhaps this —
T1 = readtable('M I T 2024 02 17.txt', 'Delimiter',{',',':'})
T1 = 15×6 table
Var1 Var2 Var3 Var4 Var5 Var6 _____ _______ ______________ ______ ______________ ______ {'k'} 0.1 {'omega_real'} 20001 {'omega_imag'} 11284 {'k'} 0.30101 {'omega_real'} 2208.3 {'omega_imag'} 1245.3 {'k'} 0.50202 {'omega_real'} 794.57 {'omega_imag'} 447.73 {'k'} 0.70303 {'omega_real'} 405.65 {'omega_imag'} 228.3 {'k'} 0.90404 {'omega_real'} 245.71 {'omega_imag'} 138.06 {'k'} 1.1051 {'omega_real'} 164.78 {'omega_imag'} 92.404 {'k'} 1.3061 {'omega_real'} 118.25 {'omega_imag'} 66.15 {'k'} 1.5071 {'omega_real'} 89.057 {'omega_imag'} 49.681 {'k'} 1.7081 {'omega_real'} 69.551 {'omega_imag'} 38.676 {'k'} 1.9091 {'omega_real'} 55.875 {'omega_imag'} 30.96 {'k'} 2.1101 {'omega_real'} 45.918 {'omega_imag'} 25.342 {'k'} 2.3111 {'omega_real'} 38.444 {'omega_imag'} 21.126 {'k'} 2.5121 {'omega_real'} 32.692 {'omega_imag'} 17.88 {'k'} 2.7131 {'omega_real'} 28.17 {'omega_imag'} 15.329 {'k'} 2.9141 {'omega_real'} 24.551 {'omega_imag'} 13.287
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')
.

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by