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.

 採用された回答

Star Strider
Star Strider 2017 年 7 月 11 日

0 投票

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 件のコメント

Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
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.
Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
Here is the attached file for read_tsf. It is pretty short and mostly just comments.
Guillaume
Guillaume 2017 年 7 月 11 日
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.
Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
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.
Star Strider
Star Strider 2017 年 7 月 11 日
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.
Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
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
Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
Should I now save it as tsf_dbl.m ?
Star Strider
Star Strider 2017 年 7 月 11 日
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.
Patrick Lydon
Patrick Lydon 2017 年 7 月 11 日
Thank you very much, it works great!
Star Strider
Star Strider 2017 年 7 月 11 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2017 年 7 月 11 日

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!

Translated by