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
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
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')
F = 6×1 string 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))"
% add '@(x)' to convert into functions
F = "@(x) " + F
F = 6×1 string array
"@(x) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268))" "@(x) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634))" "@(x) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634))" "@(x) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634))" "@(x) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634))" "@(x) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000))"
% 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
F_eqn = 6×1 cell array
{ @(x)-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268))} {@(x)-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634))} { @(x)-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634))} { @(x)-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634))} {@(x)-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634))} { @(x)-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000))}
% evaluate function 2 with random input
x_test = rand(2,1);
my_sol = F_eqn{2}(x_test)
my_sol = -2.5815e-04

更多回答(1 个)

Jan
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 .

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by