Count number of variables in netcdf file - matlab

I have a netcdf file
ncdisp('F:\Data\Coriolis\ArgoProfiles\ArgoProfiles\DataSelection_20191119_093255_9342316/argo-profiles-2902636.nc')
Source:
F:\Data\Coriolis\ArgoProfiles\ArgoProfiles\DataSelection_20191119_093255_9342316\argo-profiles-2902636.nc
Format:
classic
Global Attributes:
title = 'Argo float vertical profile'
institution = 'CSIO'
source = 'Argo float'
history = '2019-11-19T09:33:19Z creation'
references = 'http://www.argodatamgt.org/Documentation'
comment = ''
user_manual_version = '3.03'
Conventions = 'Argo-3.0 CF-1.6'
featureType = 'trajectoryProfile'
Dimensions:
DATE_TIME = 14
STRING256 = 256
STRING64 = 64
STRING32 = 32
STRING16 = 16
STRING8 = 8
STRING4 = 4
STRING2 = 2
N_PROF = 2
N_PARAM = 3
N_LEVELS = 71
N_CALIB = 1
N_HISTORY = 6 (UNLIMITED)
Variables:
DATA_TYPE
Size: 16x1
Dimensions: STRING16
Datatype: char
Attributes:
long_name = 'Data type'
_FillValue = ' '
FORMAT_VERSION
Size: 4x1
Dimensions: STRING4
Datatype: char
Attributes:
long_name = 'File format version'
_FillValue = ' '
HANDBOOK_VERSION
Size: 4x1
Dimensions: STRING4
Datatype: char
Attributes:
long_name = 'Data handbook version'
_FillValue = ' '
And I would like to count the number of variables. In this case, the answer would be 3.
I tried to use
info=ncinfo(FilePath);
numel(info.Variables.Name)
But this gives me
2.6413e+96
Warning: Number of elements exceeds maximum flint 2^53-1.
The result may be inaccurate.
Which is not correct.
How do I find the number of variables in my netcdf file? My original netcdf file is a lot bigger and I can't count them by hand anymore.

#obchardon is right.
numel(info.Variables) counts the number of variables in the netcdf file.

Related

MATLAB filename separation

In filename "name" like '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048' I need to separate
'10_m1_m2_const_m1_waves_20_90_m2_waves_90_20' from '20200312_213048'
name_sep = split(name,"_");
sep = '_';
name_join=[name_sep{1,1} sep name_sep{2,1} sep .....];
is not working, because a number of "_" are variable.
So I need to move a file:
movefile([confpath,name(without 20200312_213048),'.config'],[name(without 20200312_213048), filesep, name, '.config']);
Do you have any idea? Thank you!
Maybe you can try regexp to find the starting position for the separation:
ind = regexp(name,'_\d+_\d+$');
name1 = name(1:ind-1);
name2 = name(ind+1:end);
such that
name1 = 10_m1_m2_const_m1_waves_20_90_m2_waves_90_20
name2 = 20200312_213048
Or the code below with option tokens:
name_sep = cell2mat(regexp(name,'(.*)_(\d+_\d+$)','tokens','match'));
which gives
name_sep =
{
[1,1] = 10_m1_m2_const_m1_waves_20_90_m2_waves_90_20
[1,2] = 20200312_213048
}
You can use strfind. Either if you have a key that is always present before or after the point where you want to split the name:
nm = '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048';
key = 'waves_90_20_';
idx = strfind(nm,key) + length(key);
nm(idx:end)
Or if you know how may _ are in the part that you want to have:
idx = strfind(nm,'_');
nm(idx(end-2)+1:end)
In both cases, the result is:
'20_20200312_213048'
As long as the timestamp is always at the end of the string, you can use strfind and count backwards from the end of the string:
name = '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048';
udscr = strfind(name,'_');
name_date = name(udscr(end-1)+1:end)
name_meta = name(1:udscr(end-1)-1)
name_date =
'20200312_213048'
name_meta =
'10_m1_m2_const_m1_waves_20_90_m2_waves_90_20'

how to read broken numbers on two lines in a text file in matlab?

how to read broken numbers on two lines in matlab?
I am generating some results in text files that are being broken into two lines. Example:
text x = 1.
2345 text
What would a code look like to read the value x = 1.2345?
Suppose the value of x = 1.2345 is in file named name.txt.
When it doesn't break the values I'm looking for:
text x = 1.2345 text
I use the following (working) code:
buffer = fileread('name.txt') ;
search = 'x = ' ;
local = strfind(buffer, search);
xvalue = sscanf(buffer(local(1,1)+numel(search):end), '%f', 1);
You can remove line breaks (and other "white space", if needed) before parsing the string:
>> str = sprintf('text x = 1.\n2345 text')
str =
'text x = 1.
2345 text'
>> str = regexprep(str, '\n', '')
str =
'text x = 1.2345 text'

Passing value with numeric format

i have problems regarding passing value with formatting issues.
The description i have put in the codes as i am having problem passing my PreEditedCheque in my code for the if ValidateMMonCheque <> MM part. The output for if length(RawChequenumber) = 15 will be in 1 digit instead of 00001 ( example)
MM = HostGetFLD('','MM')
YY = HostGetFLD('','YY')
PreEditedCheque = substr(RawChequenumber,11,5)
ValidateMMonCheque = substr(RawChequenumber,7,2)
if ValidateMMonCheque <> MM Then *From this statement*
Do
PreEditedCheque = substr('00000',1,5) *This part where those 0 can't be properly shown if pass to the next statement*
EditedCheque = '00'||'2'||'0'||YY||MM||'00'||PreEditedCheque
rc = message(2,2,EditedCheque)
End
if length(RawChequenumber) = 15 Then
EditedCheque = '00'||'2'||'0'||YY||MM||'00'||PreEditedCheque + 1 *Second statement if <>MM ran, this part, the PreEditedCheque will be not in 00001, it will be 1.
rc = PanSetCtlData('PREVIEW',EditedCheque)
What you're asking for is to have the cheque number padded to the left with zeroes in a 5-character field. The Right() function is your friend:
Right(PreEditedCheque, 5, '0') /* "1" -> "00001" */

Extraction of data from DWT subband

I am attempting to extract data from a DWT subband. I am able to embed data correctly (I have followed it in the debugger),cal PSNR etc. PSNR rate seem very high 76.2?? however,I am having lot of trouble extracting data back!It is sometimes extracting the number 128?? Can anyone help or have any idea why this is? I would be very thankful.I have been working on this all day & having no luck!I am very curious to know??
Data Embedding:
coverImage = imread('lena.bmp');
message = importdata('minutiaTest.txt');
%message = 'Bifurcations:';
[LL,LH,HL,HH] = dwt2(coverImage,'haar');
if size(message) > size(coverImage,1) * size(coverImage,2)
error ('message too big to embed');
end
bit_count = 0;
steg_coeffs = [4, 4.75, 5.5, 6.25, 7];
for jj=1:size(message,2)+1
if jj > size(message,2)
charbits = [0,0,0,0,0,0,0,0];
else
charbits = dec2bin(message(jj),8)';
charbits = charbits(:)'-'0';
end
for ii=1:8
bit_count = bit_count + 1;
if charbits(ii) == 1
if HH(bit_count) <= 0
HH(bit_count) = steg_coeffs(randi(numel(steg_coeffs)));
end
else
if HH(bit_count) >= 0
HH(bit_count) = -1 * steg_coeffs(randi(numel(steg_coeffs)));
end
end
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'newStego.bmp');
Data Extraction:
new_Stego = imread('newStego.bmp');
[LL,LH,HL,HH] = dwt2(new_Stego,'haar');
message = '';
msgbits = '';
for ii = 1:size(HH,1)*size(HH,2)
if HH(ii) > 0
msgbits = strcat (msgbits, '1');
elseif HH(ii) < 0
msgbits = strcat (msgbits, '0');
else
return;
end
if mod(ii,8) == 0
msgChar = bin2dec(msgbits);
if msgChar == 0
break;
end
msgChar = char (msgChar);
message = [message msgChar];
msgbits = '';
end
end
The problem arises from reading your data with importdata.
This command will load the data to an array. Since you have 39 lines and 2 columns (skipping any empty lines), its size will be 39 2. However, the program assumes that your message will be a string. For example, 'i am a string' has a size 1 13. This expectation of the program compared to the data you actually give it creates all sorts of problems.
What you want is to read your data as a single string, where the number 230 is not one element, but 3 individual characters. Tabs and newlines will also be read in as well.
To read your file:
message = fileread('minutiaTest.txt');
After you extract your message, to save it to a file:
fid = fopen('myFilename.txt','w');
fprintf(fid,message);
fclose(fid);

matlab: legend; CAT arguments dimensions are not consistent

I have a weird problem... I can't make legend: Lets say I have:
fraction1Str = strcat('F1 = ', sprintf('%g',round(fraction1*100)/100), '+/-',sprintf('%g',round(fraction1STD*100)/100));
fraction2Str = strcat('F2 = ', sprintf('%g',round(fraction2*100)/100), '+/-',sprintf('%g',round(fraction2STD*100)/100));
fraction3Str = strcat('F3 = ', sprintf('%g',round(fraction3*100)/100), '+/-',sprintf('%g',round(fraction3STD*100)/100));
fractionStr = [fraction1Str; fraction2Str; fraction3Str];
...
hleg = legend(fractionStr);
That is giving me error: CAT arguments dimensions are not consistent.
All strings are the same format, even number of chars... Interestingly if I change to one of i.e.
fractionStr = [fraction3Str; fraction3Str; fraction3Str];
fractionStr = [fraction2Str; fraction2Str; fraction2Str];
fractionStr = [fraction1Str; fraction1Str; fraction1Str];
its working, but no with these three different strings... Any ideas?