怎么把matlab工作区保存为c文件夹,将变量从函数保存到工作区(matlab)(Saving variables from a function to the workspace (matlab))...

将变量从函数保存到工作区(matlab)(Saving variables from a function to the workspace (matlab))

我是Matlab的初学者,所以非常感谢任何提示。

我有一个输出函数F,它在处理时创建各种过渡变量。 例如:

F=myfun(x1, x2, x3)

a=f(x1)

b=g(x2)

F=z(a,b,x1,x2,x3)

end

我希望在调用函数后,可以在工作区中查看和编辑这些过渡变量a和b。 我怎样才能做到这一点?

我玩持久和全球,但它没有奏效。

我希望将它保持为单输出函数,随后我继续使用fsolve找到F的根(F是一个非线性方程组)。

总而言之,问题是:

[x1, fval]=fsolve(@(x1) myfun(x1, x2, x3), x0)

a

Error:

Undefined function or variable 'a'.

非常感谢你对这个看似简单的问题的任何帮助。

I'm a total beginner to Matlab so would really appreciate any tips.

I have a one-output function F, which creates various transitional variables while processing. For example:

F=myfun(x1, x2, x3)

a=f(x1)

b=g(x2)

F=z(a,b,x1,x2,x3)

end

I wish to have these transitional variables a and b available for viewing and editing in the workspace after calling the function. How can I do this?

I played with persistent and global, but it didn't work.

I wish to keep it as a one-output function, as I proceed afterwards to find the roots of F with fsolve (F is a system of non-linear equations).

So in summary the problem is:

[x1, fval]=fsolve(@(x1) myfun(x1, x2, x3), x0)

a

Error:

Undefined function or variable 'a'.

Many many thanks for any help on this seemingly easy problem.

原文:https://stackoverflow.com/questions/22039648

更新时间:2019-06-02 15:35

相关问答

将变量定义为工作空间中的全局变量(第一个)和函数,并将其分配给函数。 工作空间中变量的值应在函数运行时更新。 Define the variable as global in the workspace (first) and the function and assign it in the function. The value of the variable in the workspace should get updated as the function runs.

是的。 使用fieldnames名以编程方式获取变量名称,并使用assignin将它们粘贴到工作区中。 function struct2vars(s)

%STRUCT2VARS Extract values from struct fields to workspace variables

names = fieldnames(s);

for i = 1:numel(names)

assignin('caller', names{i}, s.(names{i}));

end

将该函数称为

...

指定确切的文件名,包含扩展名。 并更好地使用将接受输入的变量。 function test

fun1

clear input

data=load('test.mat');

plot(data.input)

return

end

function fun1

input=1:10;

save('test.mat','input')

return

end

如果您想使用相同的数据结构,您可以使用: data.input=1:10;

s

...

正如Dan在评论中所指出的,将包含全局变量的mat文件加载到结构中会剥离全局属性。 foo = load('settings.mat');

要解决全局问题,对其余代码产生影响最小,生成和使用settings.mat文件的代码,然后您可以提取所需的字段: foo = load('settings.mat');

settings = foo.settings;

这将删除全局属性,并声明设置变量的来源。 (这在以后执行不可避免的代码考古时确实有帮助)。 As noted by Dan in the

...

他文件是3d matrix of 64 bits double值的3d matrix of 64 bits double 。 它只是加载到你的工作区。 你可以用它的名字来叫它。 你可以调用它的一个子矩阵。 例如, pwr(1,1,1)

要么 pwr(1)

将打印它的第一个值。 另外,你可以看到它的子矩阵: a=pwr(:,:,1);

会给你一个来自pwr的二维矩阵,其中有1800个元素。 工作区显示3D +矩阵的最多0.5 M像素值。 He file is a 3d matrix of 6

...

关于你的clearvars问题,那是因为这个函数在设计时只在被调用的范围内执行请求的清理。 通过解决工作区变量不是一个好主意(您有更好的选择,比如将数据存储在GUI句柄中,并在完成时清除这些数据)......这是一个基于您的示例的片段,向您展示了如何实现你的目标: function myFunction()

% perform some calcunations...

evalin('base','clearvars(''-except'',''density'')');

end

...

您可以使用assignin将变量保存在基本工作区中,然后使用evalin 将其恢复。 assignin('base', 'imgFixed', handles.imgFixed) - 将handles.imgFixed导出到基础工作区 您还可以将handles.imgFixed分配给全局变量 。 通过这种方式,您可以从其他所有功能访问它。 globale imgFixed; imgFixed = handles.imgFixed; 但我不知道我是否完全理解你的问题。 为什么要将这些图像存储在工作区

...

您正在使用Inport并使用变量名称对其进行命名,但它不会影响变量,它只是一个端口。 尝试使用常量块并将其设置为变量名称(例如“a”) You are using an Inport and you named it with the variable name but it does not affect the variable to it, it is only a port. Try using the Constant block and set it to your variable

...

“静态”意味着固定,“工作空间”是Matlab称之为存储所有变量的地方。 对于非嵌套函数,当Matlab位于函数的开头时,工作空间开始为空; 随着Matlab继续通过函数的代码行,它不断向工作区添加更多变量。 对于具有嵌套函数的函数,Matlab首先解析函数以查看将创建的变量(它专门查找x =类型行),然后创建所有这些变量(值为“未分配”),然后仅执行它开始贯穿代码; 但是在运行代码时,它永远不会创建新的变量。 这就是代码的原因 function TestNestedFunction

syms x

...

怎么样: x = evalin('caller', 'x')

但是,Matlab的文档存在一个限制: evalin不能递归地用于评估表达式。 例如,形式为evalin('caller','evalin(''caller'',''x'')')的序列不起作用。 然而, evalin不是一个很好的功能。 我应该避免使用任何用法。 How about: x = evalin('caller', 'x')

There is a limitation however, from Matlab's docu

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值