Getting an array from another function
Show older comments
Basically, I have a function called read_tsf which reads a time stamp file that you choose and puts it into a 48x1 matrix. I am currently writing a program that analyzes data and plots it accordingly. I need to use the time stamp file matrix in this code to mark points on the plot. I tried just calling the function in the code "read_tsf;" and then tried retrieving the matrix later in the code "tsf_dbl();" but it gives me an error saying "Undefined function or variable 'tsf_dbl'." So I am obviously doing this wrong. How do I use my function read_tsf in order to retrieve the matrix and use it in another program? Thank you.
Accepted Answer
More Answers (1)
Guillaume
on 11 Jul 2017
That function is really not well written. Here is a better version with all the useless operations removed:
function timestamps = read_tsf(filepath)
%Imports & parses data from BINARY TimeFile
%filepath: full path of file to read. If not specified or empty, the function prompts for a file
if nargin == 0 || isempty(filepath)
[filename, path] = uigetfile({'*.tsf', 'All Files (*.tsf)'}, 'Choose a Binary Data File');
if filename == 0
error('operation cancelled by user');
end
filepath = fullfile(path, filename);
end
fid = fopen(filepath, 'r', 'l');
if fid == -1
error('Failed to open file %s', filepath);
end
timestamps = fread(fid, Inf, 'double');
fclose(fid);
end
To use that function you have to give it an output. e.g. in your code:
tsf_dbl = read_tsf; %function will prompt for file
%or
tsf_dbl = read_tsf('C:\somewhere\somefile') %function will read specified file
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!