write a xlsx file

Hello folks,
I have a mat file structed as follow and I would like to convert it to xls format.
k = load('psnr_ssim_Stokes_Double_Difference.mat')
k =
struct with fields:
psnr_value_I: [7-D double]
psnr_value_Q: [7-D double]
psnr_value_U: [7-D double]
ssim_value_I: [7-D double]
ssim_value_Q: [7-D double]
ssim_value_U: [7-D double]
I used this code but it didn't give me the content of the variables in a columns I got them in row. This is the code that I used
filename = 'psnr_ssim.xlsx'
T = table(ssim_value_I,psnr_value_I,ssim_value_Q,psnr_value_Q,ssim_value_U,psnr_value_U)
writetable(T,filename)

12 Comments

Scott MacKenzie
Scott MacKenzie on 21 Jul 2021
Edited: Scott MacKenzie on 21 Jul 2021
I'm a bit confused by your code. You are creating a table by specifying only the fields of the structure. That should generate a compile error. You need
T = table(k.ssim_value_I, . . . );
or just
T = table(k);
It might help if you post your data or post the complete code you are using along with any errors generated.
assia assia
assia assia on 21 Jul 2021
Edited: assia assia on 21 Jul 2021
close all;
clear all;
clc;
tmp_g=1;
for gain=[1000000000, 10000000000]
tmp_r = 1;
for real = [1,2,3,4,5]
tmp_a =1;
for a= [0.2,1.5,0.8500000000000001]
tmp_i = 1;
for incl = [0.0,45.0,90.0]
tmp_p = 1;
for pa = [0.0,90.0,180.0]
tmp_e=1;
for e = [0.0,0.3,0.6]
tmp_w=1;
for wave=[900,1200,1600,2200]
if a==0.8500000000000001
tit_stokes = sprintf('StokesParameters_Synthetic_Star_Gain_%d-Real_%d-Int_a=0.8500000000000001_incl=%3.1f_pa=%3.1f_e=%3.1f_wave=%d.fits',...
gain,real,incl,pa, e, wave);
Stokes = fitsread(tit_stokes);
tit_double_ratio = sprintf('Linear_Separable_Model_Synthetic_Star_Gain_%d-Real_%d-a_Int_a=0.8500000000000001_incl=%3.1f_pa=%3.1f_e=%3.1f_wave=%d.fits',...
gain,real,incl,pa, e, wave);
double_ratio = fitsread(tit_double_ratio);
else
tit_stokes = sprintf('StokesParameters_Synthetic_Star_Gain_%d-Real_%d-Int_a=%3.1f_incl=%3.1f_pa=%3.1f_e=%3.1f_wave=%d.fits',...
gain,real,a,incl,pa, e, wave);
Stokes = fitsread(tit_stokes);
tit_double_ratio = sprintf('Linear_Separable_Model_Synthetic_Star_Gain_%d-Real_%d-a_Int_a=%3.1f_incl=%3.1f_pa=%3.1f_e=%3.1f_wave=%d.fits',...
gain,real,a,incl,pa, e, wave);
double_ratio = fitsread(tit_double_ratio);
end
ssim_value_I(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = ssim(Stokes(:,:,1),double_ratio(:,:,4));
psnr_value_I(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = psnr(uint8(Stokes(:,:,1)), uint8(double_ratio(:,:,4)));
ssim_value_Q(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = ssim(Stokes(:,:,2),double_ratio(:,:,5));
psnr_value_Q(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = psnr(uint8(Stokes(:,:,2)),uint8(double_ratio(:,:,5)));
ssim_value_U(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = ssim(Stokes(:,:,3),double_ratio(:,:,6));
psnr_value_U(tmp_g,tmp_r, tmp_a, tmp_i, tmp_p, tmp_e, tmp_w) = psnr(uint8(Stokes(:,:,3)),uint8(double_ratio(:,:,6)));
tmp_w=tmp_w+1;
end
tmp_e=tmp_e+1 ;
end
tmp_p=tmp_p+1 ;
end
tmp_i=tmp_i+1;
end
tmp_a=tmp_a+1;
end
tmp_r=tmp_r+1;
end
tmp_g=tmp_g+1;
end
save('psnr_ssim_Stokes_Linear_Seperable.mat','ssim_value_I','psnr_value_I','ssim_value_Q','psnr_value_Q','ssim_value_U','psnr_value_U')
Here's the full code.
Probably not
T = table(k);
More likely would be
T = struct2table(k);
which would create a table with one row, with variables named after the field names, and with each entry being a 7D array.
If you create a table object containing variables that are not vectors, then when you go to write it out using writetable(), each column will be made into a new variable, like
array2table(reshape(k.psnr_value_I, size(k.psnr_value_I,1), []))
but with variable names derived from the original variable name.
Are you sure that is what you want?
Excel really is not designed for arrays with more than 2 dimensions.
or csv. It isn't a problem, just I would like a table with coulmns that contain my defiened variables
if a==0.8500000000000001
Never compare floating point numbers for equality, except under the following circumstances:
  • in some cases, it is important to know if the number is exactly 0, such as to prevent division by 0
  • You can compare for equality if both numbers are derived from the same source, such as testing (x == min(x))
  • if the floating point numbers are all really integers and are never calculated as non-integers, then you can compare the values for equality. For example, never (0:1/3:1)*3 compared to an integer, as the 1/3 suffers floating point round-off. If you need 0:1/3:1 then it is safer to use (0:3)/3
  • comparing to literal 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875 are safe, but only provided that the values were set by literals, not by calculations that "should" mathematically result in one of the values
So, instead of
tmp_a =1;
for a= [0.2,1.5,0.8500000000000001]
stuff
tmp_a = tmp_a + 1;
end
use something like
a_vals = [0.2,1.5,0.8500000000000001];
for tmp_a = 1 : length(a_vals)
a = a_vals(tmp_a);
stuff
end
and then instead of comparing a==0.8500000000000001 compare indices, tmp_a == length(a_vals)
thank you. I see, it's a good approach.
You can move all the lines similar to
a_vals = [0.2,1.5,0.8500000000000001];
to before the loops.
You will find that it often helps to calculate the number of elements in one of those vectors, and then loop to that variable, such as
num_a = length(a_vals);
stuff
ssim_value_I = zeros(num_g, num_r, num_a, num_i, num_p, num_e, num_w);
stuff
for temp_a = 1 : num_a
a = a_vals(tmp_a);
stuff
end
Now I have 2x1620 table. I would like to be a columns for each variable, not 1620 rows.
What is your current code for creating the table object?
assia assia
assia assia on 21 Jul 2021
Edited: assia assia on 21 Jul 2021
data = load("data.mat")
k = struct2table(data)
writetable(k,'data.csv')
Is "data.mat" the same file as in
save('psnr_ssim_Stokes_Linear_Seperable.mat','ssim_value_I','psnr_value_I','ssim_value_Q','psnr_value_Q','ssim_value_U','psnr_value_U')
? If so then 'ssim_value_I' is length(g_vals) by 6 other dimensions, and you have two different g_vals, so ssim_value_I would be 2 x 6 other dimensions. When you do the struct2table() the 2 x something becomes a table with two rows.
You can convert to individual variables by using splitvars(); see https://www.mathworks.com/help/matlab/ref/table.splitvars.html
... but keep in mind that you will then have a table with a lot of different columns, each of which is only 2 rows long.
I would suggest to you that you should be considering permute() and then reshape() to get the data in the order you want.

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 21 Jul 2021

Commented:

on 21 Jul 2021

Community Treasure Hunt

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

Start Hunting!