Getting an array from another function

5 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2017-7-11
You did not say how you are calling either ‘read_tsf’ or ‘tsf_dbl’. Functions have their own workspaces that they do not share with the base workspace. Your ‘read_tsf’ function must output the vector to the base workspace in order for other functions to use it.
Try something like this:
ts_vct = read_tsf(filename);
ts_num = tsf_dbl(ts_vct);
  10 个评论

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2017-7-11
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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by