You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Code to read files from ansys fluent and perform calculations
7 views (last 30 days)
Show older comments
hello expert,
Am new to matlab. please i need help on how to generate a code that would claculate this equation.
D = C x P ^a x t^b
the vaues of a, b, c are constants with values c =3.62e-7; a =2.416; b = 0.785
the values of P and t are in a .txt file output in columns as attached. t represent time and p represent stress.
please your guidance will help alot
best regards,
29 Comments
dpb
on 28 Jul 2020
See doc on importdata, readmatrix, etc., ... plenty of examples for simple text files...
Use the "dot" operators for element-wise arithmetic operations and just write the equation as it stands.
There's also much "Getting Started" doc that illustrates...
Victor Albert
on 28 Jul 2020
do you mean.
fidi = fopen('file3.xy,'rt');
st = fseek(fidi, 1, 'bof'); % Position File After First Line
k1 = 1; % Counter
while (st == 0) && (~feof(fidi)) % Test For End Of File Or Unsuccessful ‘fseek’ File Position
prtcl{k1} = textscan(fidi, '%f%f', 'Delimiter','\t', 'HeaderLines',4, 'CollectOutput',1);
st = fseek(fidi, 50, 'cof'); % Position File Pointer To Next Line After Stop
k1 = k1 + 1;
end
c =3.62e-7;
a =2.416;
b = 0.785
D = C x P ^a x t^b
plot(P1(:,1),P1(:,2)); hold on; grid on;
plot(P2(:,1),P2(:,2)); hold on; grid on;
plot(P3(:,1),P3(:,2)); hold on; grid on;
plot(P4(:,1),P4(:,2)); hold on; grid on;
plot(P5(:,1),P5(:,2)); hold on; grid on;
xlabel('Time(secs)');
ylabel('Stress(Pa)');
title('Time vs Scalar time')
dpb
on 28 Jul 2020
Edited: dpb
on 29 Jul 2020
"do you mean."
Well, no, not really.
We can't do anything with images and the filename in the image is a .xls file which isn't text, but...that's not the filename you use above, either. We've no klew really, but--using the image as a guess as to what the file contains
fnD=@(t,P) 3.62e-7*P.^2.416.*t.^0.785; % set anonymous function for "D"
data=importdata('file3.xy');
data=data.data; % dereference the struct data field to array
D=fnD(data(:,1),data(:,2));
...
Do whatever w/ the results after...
Doesn't look like you had done any reading on anything I suggested earlier...
PS: If would want somebody to really look at, attach the input data file; as noted, there's nothing anybody can do with an image other than just look at it.
Victor Albert
on 28 Jul 2020
hello sir dpd,
see attached file as requested. i really appreciate your input.
dpb
on 28 Jul 2020
data=importdata('scalarvstime1.txt');
I so rarely use importdata I forgot it returns a struc with the actual numeric data in the field data. So, first have to dereference it to get the actual data array.
>> PT=data.data; % the t,P data array
>> whos PT
Name Size Bytes Class Attributes
PT 4009x2 64144 double
>> PT(1:10,:) % looks like |importdata| found the right start point on its own...
ans =
0 0.2910
0.0000 0.2911
0.0001 0.2953
0.0001 0.2980
0.0002 0.2795
0.0002 0.2663
0.0003 0.2894
0.0004 0.3020
0.0005 0.2810
0.0005 0.2731
>> figure
>> plot(PT(:,1),PT(:,2))

is P vs t.
Somethings seems to be amiss with the time vector -- it's not monotonically increasing it appears -- can check on that easily enough--
>> find(diff(PT(:,1))<=0)
ans =
165
330
419
573
830
1145
1305
1471
1642
1811
1976
2137
2604
2770
2931
3093
3250
3421
3599
3793
>>
So, what are we to do about the time? Are there different transients in the file, is the output interwoven by time by some other variable so should sort() on time and rearrange, ...???
We "know nuthink!" to quote Sgt Schultz about what is in the file...
dpb
on 28 Jul 2020
Edited: dpb
on 28 Jul 2020
tz=find(PT(:,1)==0); tz=[tz;size(PT,1)]; % get the zero time points in file; augment numel
figure,hold on % prepare to plot traces on new figure
for i=1:2:numel(tz)-1 % every other sequence starting t==0
t1=tz(i); t2=tz(i+1)-1; % look up the time points start, end
hL(i)=plot(PT(t1:t2,1),PT(t1:t2,2)); % and plot P vs t for segment
end
hAx.YScale='log'; % so can separate visually
ylim([0.01 25])
grid on
hLg=legend(compose('Section %d',[1:numel(tz)/2].'),'Location','southeast');
results in

So, there are 20 different traces in the one file, each beginning at t=0.
The above plot is every other one of the 20; 1,3,5,...
What they are we've no klew; only you know what you have in the simulation.
ADDENDUM/ERRATUM:
Actually, as noted below there are 20 different time traces in the file, not 10; the above is every other one; the plot is far too busy to try to show all 20. The legend should have been
hLg=legend(compose('Section %d',[1:2:numel(tz)].'),'Location','southeast');
so the sections are really 1,3,5,...
Victor Albert
on 28 Jul 2020
the .txt file is an output result from Ansys simulation run to get the variable of P and t.
t represent time and P represent shear on the .txt file i sent.
its a kind of particle tracked from a simulation carried out put.
am looking at having a plot for D vs time(t) and P vs time(t)
Victor Albert
on 28 Jul 2020
this is my ouput plot using microsoft excel and python.
but i feel matlab should be the best in giving a better visualization.

dpb
on 28 Jul 2020
The above plots ARE what is contained in the file -- the first blue line is all of P vs t on one graph -- it clearly demonstrates the discontinuity in the time trace.
The table below it shows the points in the file at which time is reset to zero; there are 20 of those in the file; I plotted the data P vs t for every other one of those sections in the subsequent figure.
I showed you above how to compute D from the given t,P; that won't be affected by the discontinuity in the time vector but it will ALWAYS have a discontinuity on a time axis if you don't separate out where in the file the time measurement starts over again.
Again, we don't know anything else; all we can do is read the file and show what's in it--what it means (if anything) is beyond our ken or ability to do anything else.
You're not going to get a plot that looks anything at all like your sample from these data, though...just not agonna' happen.
Victor Albert
on 28 Jul 2020
hello dpb, nice plot! thumbs up for you sir. please i have atatched the matlab code for your revised, seems am getting it wrong here.
please review and update for me. thanks.
dpb
on 28 Jul 2020
The P, D plot for the top trace (5th time sequence in the file) from above figure looks like;

As exepected D mirrors P but is much lower in magnitude
Victor Albert
on 28 Jul 2020
please the matlab file i sent can you review and correct my wrongs! i at least this would enable other folks out here to learn from this. thanks.
dpb
on 28 Jul 2020
fnD=@(t,P) 3.62e-7*P.^2.416.*t.^0.785; % set anonymous function for "D"
data=importdata('file3.xy');
PT = data.data;
D=fnD(PT(:,1),PT(:,2));
%plot(PT(:,1),PT(:,2)) % this plots all data as one trace--ignoring the starting over of time inside the file
tz=find(PT(:,1)==0); tz=[tz;size(PT,1)+1];
figure,hold on
for i=1:2:numel(tz)-1 % this only plots every other one starting at 1. 1,3,5,...
t1=tz(i); t2=tz(i+1)-1;
hL(i)=plot(PT(t1:t2,1),PT(t1:t2,2));
end
hAx.YScale='log';
ylim([0.01 25]) % this scaling may only be valid for this subset; don't know max in the whole file
grid on
hLg=legend(compose('Section %d',[1:2:numel(tz)].'),'Location','southeast');
Minimalist cleanup --
- Only plot P for odd sequences of time==0 sequences; D would be same subscripts
- Change the "2" in the step sequence in the for loop to plot all or
- Start with i=2:2: ... to do even sequences
- Scaling will be dependent on which sequences are plotted; this is ok for odd done here.
I have no idea how you would have gotten something looking like the plot you showed above -- there's no way D could be on order of 10^4 with the coefficients you provided of c=3.6E-7 as the multiplier.
For the peak shown at t=0.1 to be 32,500,
> fsolve(@(x) fnD(0.1,x)-32500,75000)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
7.2196e+04
>>
The P value would have had to have been 72,200. However, the data in the file is
>> max(PT(:,2))
ans =
91.3812
>>
Makes no sense at all. Either your data isn't the same data (or even close) or the correlation isn't what you wrote in initial post or you just erred.
dpb
on 29 Jul 2020
BTW. all the transients on log-log axes looks like

The D variable would mimic those with the noted scale factor. This does show the one with the large peak magnitude of about 90 not in the odd-numbered set before.
Victor Albert
on 29 Jul 2020
the total rows (with 2columns) for time and P values are up to 4009 as checked with excel.when i tried running the code as per attached picture, i dont get the required plots, " blank screen with figure1". what could be the problem and how do i go about fixing this.
i was thinking of applying the below "cell2mat" function.
dpb
on 29 Jul 2020
It ran here; you see the output attached.
Use debugger to find where there's a problem.
There's no need for cell2mat; no cell arrays are generated.
Victor Albert
on 29 Jul 2020
below are the likely error am getting for the figure not poping up i guess. how do i rectify this, please am new

Victor Albert
on 29 Jul 2020
please can you repaste your current code that gave you plot let me try it and check one-by-one where the error is coming from . i still have not gotten the right plot/graph.
that would help and save your time
dpb
on 29 Jul 2020
You've got to learn your way around MATLAB to get anywhere...can't rely on somebody else forever for the simplest of debugging.
See above hint about current axes...try
doc current
lookfor current
and see what looks to be related...
Working through the "Getting Started" and "OnRamp" tutorials would pay back time invested multi-times over in speeding up the process.
Victor Albert
on 29 Jul 2020
that is why am here to learn from everyone. if its simple for you, others it might be complex to learn. please jusst correct the issues i have at the moment so i continue with with reference document. i know i will get there someday.
Victor Albert
on 5 Aug 2020
i am still having issues with this plot. can i seperate the files and resend
Rik
on 26 Aug 2020
Why did you delete all your comments on your other question?
Just in case you try that same thing here, I'll make a complete copy of this thread on the Wayback Machine.
Accepted Answer
KSSV
on 28 Jul 2020
Edited: KSSV
on 28 Jul 2020
Read about textscan.
fid = fopen('Myfile.txt','r') ;
S = textscan(fid,'%f %f\n','HeaderLines',4) ;
S = S{1} ;
fclose(fid) ;
S should be a n*2 matrix. Read baout textscan if the code is not working properly.
6 Comments
Victor Albert
on 28 Jul 2020
thats the above error i get. the text file is a particle track file imported from Ansys simulation output.
please i need your guide
KSSV
on 28 Jul 2020
Edited...there is a typo error...it should be HeaderLines. I asked you to read the documentation.
Victor Albert
on 28 Jul 2020
sir KSSV,
please does the above code takes the place of the below long piece of code. as earlier reported.
fidi = fopen('file3.xy,'rt');
st = fseek(fidi, 1, 'bof'); % Position File After First Line
k1 = 1; % Counter
while (st == 0) && (~feof(fidi)) % Test For End Of File Or Unsuccessful ‘fseek’ File Position
prtcl{k1} = textscan(fidi, '%f%f', 'Delimiter','\t', 'HeaderLines',4, 'CollectOutput',1);
st = fseek(fidi, 50, 'cof'); % Position File Pointer To Next Line After Stop
k1 = k1 + 1;
end
dpb
on 28 Jul 2020
There's no point at all in using textscan on such a simple file format... importdata does it all on its own as shown above; readmatrix would also do it w/ just 'NumHeaderlines' as additional argument.
Your attempts at positioning a file with fseek are totally misguided -- fseek moves by BYTES not records; the file is not fixed-length records so you can't count characters reliably anyway and is probably tab delimited (altho I didn't open it to check; there's no need nor sense in pursuing that depth).
If you were adamant on using textscan, as KSSV says, "READ THE DOCUMENTATION!" and just fix the typo.
There's no point in giving advice/guidance if not going to pay any attention to it... :(
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)




