.dat file to a readable file for an array in MATLAB

179 次查看(过去 30 天)
I want to read a .dat file that has columns and rows of information into a MATLAB function so I can make array indexing with the function variables, and call that function into another script. How would I be able to read the file into a function, when it is formated as .dat? Can I make my own file with the information it has inside into a readable format for MATLAB?
I want to use it for array indexing so I can define some variables withing the function
Example:
function [ A, B, C, D, E, F, G] = script#0 ( file_I_want_to_read)
^ ^
Variables This is where the file_I_want_to_read for the purpose of array indexing the contents in the file, for defining my variables
fileid = fopen( file_I_want_to_read, 'r');
format = ['%s', repmat('%{ .............ect.
K = textscan( fileid, format, 'Delimiter', '\t' );
fclose( fileid );
  4 个评论
Walter Roberson
Walter Roberson 2022-4-15
The file that you fopen()'d was not available under the name you asked for. Perhaps it is not in the current directory but you did not include the directory information?
[filename, dirname] = uigetfile('*.dat');
if ~ischar(filename)
return; %user cancel
end
fullname = fullfile(dirname, filename);
[uigffid, msg] = fopen(fullname, 'r');
if uigffid < 0
error("Could not open file '%s' because '%s'", fullname, msg);0
end
U = textscan(uigffid, format, 'Delimiter', '\t');
fclose(uigffid);

请先登录,再进行评论。

回答(1 个)

Siraj
Siraj 2023-9-12
Hi! It is my understanding that you want to read a .dat file with columns and rows of information into a MATLAB function for array indexing and variable definition.
One of the methods to read a ".dat" file is by utilizing the "textscan" function in MATLAB. This function is designed to read formatted data from a text file and store it in a cell array. You can use the "textscan" function to read ".dat" files, ensuring that you specify the appropriate formatting and only read the relevant data. The "textscan" function offers several name-value arguments that allow you to control which lines to read and which lines to skip. For more detailed information and examples, you can refer to the following link:
Consider the following example for better understanding
I have a ".dat" file named "example.dat" with the following contents:
# (EXCLUDING parts overlapped by quadrics)
#
# Dose units are eV/g per primary history
# Number of voxels in x
# 3 1 3
# Voxels size dx
# 1.00000E+01 1.00000E+00 1.00000E+01
# The granularity used to compute the voxels mass was:
# 10
#
# For plotting purposes
# the low end and the middle point of each voxel
#
# xLow(cm) : xMiddle(cm) : yLow(cm) : yMiddle(cm) : zLow(cm) : zMiddle(cm) : dose (eV/g) : +-2sigma : voxel mass (g) : Pure(+)/Overlapped(-)
# zVoxIndex=1
# yVoxIndex=1
1.2 3 4.0
1.2 3 4.0
1.2 3 4.0
# zVoxIndex=2
# yVoxIndex=1
1.5 6 7
1.5 6 7
1.5 6 7
# zVoxIndex=3
# yVoxIndex=1
1.9 8 90
1.9 8 90
1.9 8 90
The "textscan" function is used to read the contents of the file "example.dat". The data is extracted and stored in different variables.
% Open the file for reading
file = 'example.dat';
fid = fopen(file, 'r');
% Read the data using textscan and skip lines starting with '#'
datacell = textscan(fid, '%f %f %f', 'Delimiter', '\t', 'CommentStyle', '#');
% Close the file
fclose(fid);
%extracting the data read into variables.
A = datacell{1};
B = datacell{2};
C = datacell{3};
t = table(A,B,C)
The provided example can be customized based on the specific file you have. However, I hope that the example provided has helped clarify the process of using the "textscan" function to read a ".dat" file and extracting the data into different variables.

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by