How to extract data from a function or functions. Also how to export this data to excel.

37 views (last 30 days)
I have written two sets of codes. I placed them in two separate folders as program 1 and program 2.
Now with the code calling the inverse and looping. How do I extract the data? There is nothing in workspace.
Program 1 demonstrates the actual movement of the linear motion and program 2 is just the stationary output.
Is there a way to extract these data to excel?
Both programs are attached.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 14 Oct 2021
Your functions have their own name-space where the variables that are defined exists (in addition to the input variables that you feed the functions with). When the functions complete these variables cease to exist, they are no more. To get the results of interest out you have to define (and assign) some output-variables in your function-files. This typically looks something like this:
function [r_out,flag] = position_along_line(l,r0,e_line)
Here a variables r_out and _flag are defined as output from the function and have to be defined in the function. This way your function calculates something and returns the result. You'll have to adjust your "programmes" similarly. One way to get all the results from a programme is to not have it defined as a function but use it as a script - then all of the calculations will be done in the base workspace and exist after the script has finished - the main drawback is that it is easy to develop scripts that rely on other variables to exist without noticing which gives the scrip some hidden dependencies.
Also avoid the "clc, clear all" trope inside functions it is usless, in scripts it is anoying.
HTH
  7 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 18 Oct 2021
My pleasure.
(I saw your most recent question, but thought: You'll become a much better programmer by figuring that out yourself - it's not nice sort-term but on any longer time-scale than 3 days it is to be preferred. Now you can swim!)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!