remove brace indexing from 1x1 matrix

조회 수: 1 (최근 30일)
anto
anto 2022년 12월 24일
편집: Stephen23 2022년 12월 24일
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