Process .dat file, plot, scale, and put back into similar format

7 views (last 30 days)
Hi:
Have looked at fopen, textscan, importdata, running into issues, could use support...
looking to open, ignore the headers, then import this file's tabular data, read from left to right, then down: It's all Y axis data, with x starting from 0 in increments of 0.005 sec, plot the time history, resample and recale at 0.02 seconds and output the tabular x,y data and generate a similar .dat file placed in 8 columns without the x values.
Having issues with which is the best option for file importing, delimiting and data type issues and have gone thru all the examples and can't pin it down.
Any help or insight appreciated.
Michael :)

Accepted Answer

Star Strider
Star Strider on 27 May 2023
It would help to have the file rather than an image.
I would do something like this:
fidi = fopen('Hled.dat','rt');
C = textscan(fidi, repmat('%f',1,8), 'HeaderLines',16, 'Delimiter',' ', 'CollectOutput',1);
fclose(fidi);
EarthQuake = cell2mat(C);
The readmatrix function with the same 'HeaderLines' information would also work.
.
  12 Comments
michael roberts
michael roberts on 1 Jun 2023
Not sure I am allowed to add to something Accepted as an Answer or to restart it.
I noticed some interesting period, apostophy, and bracket placements in this function for row wise:
compared to column wise:
...was that the intent?
And to follow up with initial post: Would fprintf be the way to export EarthQuakerv back to a .dat file type with predetermined columns similar to the begining and the numerical data format?
-this is so that a scale factor can be applied to EarthQuakerv, a new scaled time history geenrated, then put back into a format at 0.005 sec for use in the Shake table.
Star Strider
Star Strider on 2 Jun 2023
Yes, it was.
The first reshape call concatenated the columns as written. The second reshape call first had to transpose the ‘EarthQuake’ matrix, do the same sort of operation, then transpose the result to be a column vector. That all has to do with how reshape works.
If you want to save the result, I would do something like this:
EarthQuakeTable = table(t{:}, EarthQuakerv, 'VariableNames',{'Time_sec','Acceleration'}) % Create ‘table’
writetable(EarthQuakeTable, 'EarthQuake.txt') % Write To File
That will write it to a text file. (Include other variables such as ‘Velocity’ and ‘Displacement’ as desired.) That will be the easiest to read and import, although other options are possible. See the documentation on writetable for details.
The current online documentation is for R2023a, so check your own documentation for R2020a for the available options and requirements for it, since there have been changes to table as well in the interim.
Experiment with the ‘Displacement’ integration. Subtracting the mean of ‘Velocity’ from the ‘Velocity’ vector might give a more meaningful result.
.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 27 May 2023
Your code says to scan the file for 8 floating point numbers with commas between them with a delimiter of tab. But your file contains columns of 8 floating point numbers with no commas
importdata expects a file name rather than a file identifier with the exception that the first parameter may be the special name '-pastespecial' instead of a file name.

Steven Lord
Steven Lord on 30 May 2023
You may want to interactively experiment with the various options for importing your data in the Import Tool. You can specify the range of data to import, control whether MATLAB should interpret the file as delimited or fixed-width, and specify how you want the data to be imported into MATLAB (table, vectors, cell array, etc.)
Once you've changed the settings so the data looks the way you want, you can import the data (if you only have this one file to read in) or generate MATLAB code to import the data (if you have multiple files formatted the same way and you want to reuse the commands to import each of the files.)

michael roberts
michael roberts on 31 May 2023
That shows up with some good options I'm not 100% sure on: I.e. table vs Column vectors?
Since the data needs to read left to right, then down, and place a time value next to it starting from 0 to 0+0.005 sec.... perhaps column vectors aren't ideal: canm row vectors be generated then combined (appended) to generate the full list?
Table was '3.230996E-001' type values in a large table.
Appreciate the advice.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!