Getting an array from another function

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

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 Comments

My apologies, but I do not quite understand what you mean by this. So I will try to explain the best as possible. read_tsf is saved in the same directory as my other program called "pointsix". When I call read_tsf on its own, the output vector, "tsf_dbl", is saved to the workspace. Now when I call "read_tsf;" in pointsix, the vector is saved to the same workspace as all the other outputs of pointsix. Also, maybe this is important: when you call read_tsf it prompts the user with a dialog/browser window to search through files to choose the ".tsf" you want to read.
Here is the attached file for read_tsf. It is pretty short and mostly just comments.
Also, maybe this is important. No, it's not.
When I call read_tsf on its own, the output vector, "tsf_dbl", is saved to the workspace That is. That's not the normal behaviour of a function.
So, can you:
  • the exact line of code you use to call the function
  • confirm that read_tsf is indeed a function and not a script. That is, the first line of read_tsf.m starts with function.
  • actually can you show that first line or even better the whole body of the function.
To call the function in my pointsix program I literally only say:
read_tsf;
which prompts me to select a file once I run the pointsix program. I select the file and the array is successfully in the workspace, but later on in the code I try to use the array tsf_dbl and it says it does not exist.
Yes the read_tsf is indeed a function, I attached it to my previous comment.
I agree with Guillaume.
Does ‘tsf_dbl’ appear in your workspace browser as a variable?
I would rewrite your ‘read_tsf’ function as:
function tsf_dbl = read_tsf(varargin)
and eliminate the assignin call. This eliminates any ambiguities about what it is and where it is.
Okay, thank you. So is tsf_dbl now my function? in my pointsix program can I just simply call tsf_dbl and act as if it were an array already in the program
Should I now save it as tsf_dbl.m ?
No. Save the edited function as ‘read_tsf’, or rename it and save it as ‘read_tsf_new’ (or something more to your liking) if you want to keep the previous version unchanged. (The function name and the ‘.m’-file name must match.) You simply need to call the edited function with an output argument.
Thank you very much, it works great!

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!