Azzera filtri
Azzera filtri

Read text file line by line to columns

11 visualizzazioni (ultimi 30 giorni)
SHIMNA MANOHARAN
SHIMNA MANOHARAN il 7 Mag 2024
Modificato: Dyuman Joshi il 7 Mag 2024
Hello community,
I have the data in this form
.557 .331 .069 .065 .101 .052 .109 .045 .114 .041 .117 .041 .109 .451 .234 -.058 .347 .406 -.005 .948 .252 -.007 .140 .024 .124 .036 .113 .045 .105 .052 .097 .059 .090 .064 .085 .068 .080 .071 .077 .073 0.075
and I want the data to be read as this form
0.557
0.331
0.069
0.065
0.101
0.052
etc
Please help

Risposte (2)

Dyuman Joshi
Dyuman Joshi il 7 Mag 2024
Modificato: Dyuman Joshi il 7 Mag 2024
Use readmatrix (available from R2019a onwards) to read the data and transform it as per need -
in = readmatrix('RANCHOD - Copy.txt');
out = reshape(in.', [], 1);
disp(out)
0.5570 0.3310 0.0690 0.0650 0.1010 0.0520 0.1090 0.0450 0.1140 0.0410 0.1170 0.0410 0.1090 0.4510 0.2340 -0.0580 0.3470 0.4060 -0.0050 0.9480 0.2520 -0.0070 0.1400 0.0240 0.1240 0.0360 0.1130 0.0450 0.1050 0.0520 0.0970 0.0590 0.0900 0.0640 0.0850 0.0680 0.0800 0.0710 0.0770 0.0730 0.0750 0.0740 0.0730 0.0730 0.0720 0.0720 0.0720 0.0710 0.0710 0.0700 0.0700 0.0700 0.0690 0.0690 0.0680 0.0680 0.0670 0.0670 0.0650 0.0660 0.0640 0.0660 0.0640 0.0640 0.0640 0.0610 0.0640 0.0580 0.0660 0.0540 0.0690 0.0480 0.0740 0.0390 0.0830 0.0240 0.1040 -0.0220 0.2840 0.4070 -0.0240 0.0970 0.0250 0.0740 0.0360 0.0640 0.0410 0.0590

Pavan Sahith
Pavan Sahith il 7 Mag 2024
Modificato: Pavan Sahith il 7 Mag 2024
Hello Shimna,
I see that you are trying to read the data from a text file and convert it into a column format in MATLAB, you can follow these steps.
  1. Read the File: Use 'fopen' to open the file for reading.
  2. Read Lines and Split: Use 'fgetl' or 'fgets' in a loop to read the file line by line. Then, split the line into individual numbers.
  3. Convert to Numbers: The split parts will be strings, so convert them into numbers using 'str2double'.
  4. Store in an Array: Initialize an array before the loop and append each number to this array.
  5. Close the File: After reading all lines, close the file using 'fclose'.
This example assumes that your data is stored in a text file called data.txt.
% Open the file for reading
fid = fopen('data.txt', 'rt');
% Check if the file was opened successfully
if fid == -1
error('File cannot be opened');
end
% Initialize an empty array to store the numbers
data = [];
% Read the file line by line
while ~feof(fid)
line = fgetl(fid); % Read one line from the file
% Split the line into strings based on spaces
numbersStr = strsplit(line);
% Convert strings to numbers and remove any NaN values that might occur from spaces
numbers = str2double(numbersStr);
numbers = numbers(~isnan(numbers));
% Append the numbers to the data array
data = [data; numbers']; % Transpose to make it a column
end
% Close the file
fclose(fid);
% Display the data
disp(data);
You can refer to the following MathWorks Documentation to know more about some important functions used.
I hope this will guide you for reading and structuring data in MATLAB exactly as you envisioned

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by