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

1 view (last 30 days)
Giovanni Ponce
Giovanni Ponce on 15 Apr 2022
Commented: Walter Roberson on 15 Apr 2022
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 Comments
Walter Roberson
Walter Roberson on 15 Apr 2022
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);

Sign in to comment.

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!