remove brace indexing from 1x1 matrix
1 次查看(过去 30 天)
显示 更早的评论
I have a list of equations written with a fprintf loop and imported with importdata:
6×1 cell array
{'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) '}
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) '}
{'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000)) ' }
using
A=importdata('equations list.txt');
B=transpose(A);
C=cell2mat(B);
I get the 1x1 matrix:
'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000)) '
But i would like to remove the first and last ' ' to get this:
-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000))
in order to use it later with fsolve.
Does anybody know how to do this?
1 个评论
Stephen23
2022-12-24
编辑:Stephen23
2022-12-24
"Does anybody know how to do this?"
The single quotes are not part of the text, they are simply artifacts of the display routine (indicating the text is a character vector). It is not possible to remove what is not even there. You are confusing text with code.
As Jan correctly wrote, most likely you could simply write a function in an Mfile and supply that name/function handle of that function to FSOLVE. Then you can simply avoid the indirection of importing text and fiddling around with STR2FUNC.
采纳的回答
Karim
2022-12-24
I would recommand to read the text file into a string array, this will ease the conversion into function. See below for a demonstration
% read the text file into a string array
F = readlines('equations list.txt')
% add '@(x)' to convert into functions
F = "@(x) " + F
% convert the strings into functions
F_eqn = cell(size(F));
for i = 1:length(F)
F_eqn{i} = str2func(F(i));
end
% have a look at the content
F_eqn
% evaluate function 2 with random input
x_test = rand(2,1);
my_sol = F_eqn{2}(x_test)
0 个评论
更多回答(1 个)
Jan
2022-12-24
The quotes are not part of the contents, but shown in the command window only to show, that this is a CHAR vector. Therefore there is no need to remove the quotes.
fsolve processes functions, not CHAR vectors. Instead of importing the file, it would be much easier to convert it to an m-function: Insert the formul here:
function y = fcn(x)
y = ... % your text here
end
and save this as fcn.m .
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!