Programovanie geofyzikálnych úloh v prostredí MATLAB

Programovanie geofyzikálnych úloh v prostredí MATLAB

MATLAB (1) - vod do programovania vedeckch problmov LS 2018, 3.predn. MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 Program predmetu: 1. tde: vod, zkladn info o Matlabe, pracovn prostredie Matlabu, interaktvny reim, prv info o psan skriptov 2. tde: zkladn opercie s maticami, jednoduch import dt, zkladn grafick zobrazovanie (grafy a mapy) 3. tde: prca s reazcami, prca so sbormi 4. tde: pokroilejia grafika - popis grafov a mp, 2D grafy 5. tde: prkazy, stavba programov, M-sborov

6. tde: funkcie zabudovan v Matlabe, tvorba vlastnch funkci 7. tde: prklady programovania loh z oblasti prrodnch vied 8. tde: prklady programovania loh z oblasti prrodnch vied 9. tde: tvorba vlastnch aplikci, prca s GUI (Graphical User Interface) 10. tde: tvorba vlastnch aplikci, nstroj GUIDE pozn.: zmeny vyhraden MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 doteraz: - truktra matc: A(riadok,stlpec) - pracovn plocha (najdl. as: Command Window) - zkladn pracovn prkazy: clear;clc; ... - vznam znakov % ;

: ... - grafick prkazy: contour(), plot(), surf(), subplot(), ... - generovanie matc, aritm. opercie s maticami - natanie ASCII sborov: prkaz dlmread() MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 Obsah (3. prednka): - prca s reazcami (je potrebn pre prcu so sbormi) - prca so sbormi

Pozn.: Reazce s vlastn slov a vety (postupnosti znakov). prca s reazcami - njdenie asti reazca: strfind() Funkcia strfind() njde as reazca vo vom reazci a vrti jeho polohu/polohy (pri viacnsobnom vskyte) vo forme indexu (poradovho sla). k = strfind(str, pattern) kde str je v reazec, pattern je hadan as reazca a k je vrten index (poradov slo). Prklad: S = 'Find the starting indices of the pattern string'; indx = strfind(S, 'in')

prca s reazcami - porovnanie reazcov: strcmp() Funkcia strcmp() porovn 2 reazce; vsledkom je 0/1 (nie s identick/s identick). TF = strcmp(string,string) kde string,string s porovnvan reazce, TF je skalrna premenn s vsledkom porovnania. Prklady: odp1 = strcmp('Yes', 'No') odp2 = strcmp('Yes', 'Yes') prca s reazcami - porovnanie reazcov: strncmp() Funkcia strncmp() porovn prvch n znakov z 2 reazcov;

vsledkom je 0/1 (nie s identick/s identick). TF = strncmp(string,string,n) kde string,string s porovnvan reazce, n je poet prvch porovnvanch znakov, TF je skalrna premenn s vsledkom porovnania Prklady: odp3 = strncmp('Kansas City, KS', 'Kansas City, MO',13) odp4 = strncmp('Kansas City, KS', 'Kansas City, MO',14) prca s reazcami - vyrezanie asti reazca: strtok() Funkcia strtok() vyree as reazca zava po prv vskyt przdneho znaku (medzery) alebo inho oddelovaa. V rozrenej verzii ulo aj zvyok reazca do alej premennej. Existuj jej 3 monosti:

token = strtok(str) token = strtok(str,delimiter) [token, remain] = strtok(str,delimiter) kde str je v reazec, delimiter je oddelova, token je vyrezan as reazca a remain je zvyok reazca. Prklady: part1 = strtok('columns rows') part1 = strtok('columns_rows','_') [part1, part2] = strtok('columns,rows',',') Pozn.: Tento prkaz vyuijeme neskr pri natan dajov zo sborov. prca s reazcami- transformcia reazca na slo: str2double( Funkcia str2double() transformuje reazec s slicami na samotn slo. Existuje aj funkcia str2num(C), ktor to realizuje

s niou presnosou (iba na 4 desatinn miesta). X = str2double(str) kde str je reazec, X je premenn typu double s vsledkom transformcie. Prklady: C = '123.48'; X = str2double(C) D = 'B123.48'; Y = str2double(D) str2double('123.45e7') Pri reazci, ktor nepredstavuje platn slo je vsledkom tejto opercie vraz NaN (not a number). Transformova sa daj aj reazce s komplexnmi slami. Pozn.: Tento prkaz vyuijeme neskr pri natan dajov zo sborov.

prca s reazcami- transformcia sla na reazec: int2str () Funkcia int2str() transformuje cel slo na reazec. S = int2str(X) kde S je reazec, X je matica s celm slom. Prklady: X = 17724; S = int2str(X) X = 17723.8279736256; S = int2str(X) X = 34.4 + 17.2i; S = int2str(X) X = 0/0; S = int2str(X) Pozor, relne sla s zaokrhlovan ! Pri komplexnch len relna as!

Pozn.: Tento prkaz vyuijeme neskr pri zobrazovan dajov vo Windows oknch. prca s reazcami- transformcia sla na reazec: num2str () Funkcia num2str() transformuje relne slo na reazec. S = num2str(X) kde S je reazec, X je matica s relnym slom. Prklady: X = 17724; S = num2str(X) X = 17723.8279736256; S = num2str(X) X = 34.4 + 17.2i; S = num2str(X) X = 0/0;

S = num2str(X) Relne sla s zaokrhlovan na 3 desatinn sla. Pozn.: Tento prkaz vyuijeme neskr pri zobrazovan dajov vo Windows oknch. MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 Obsah (3. prednka): - prca s reazcami - prca so sbormi MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 natanie matc zo sborov (najjednoduchie, ASCII): vyuitie prkazu dlmread() (doteraz):

dlmread(nzov sboru,delimiter) existuje ekvivalentn prkaz importdata(): importdata(nzov sboru,delimiter); natanie matc zo sborov (najjednoduchie, ASCII): prklad: natanie anomlnych magnetickch dajov z observatria (vzorkovanch v mesanch priemeroch) clear all, close all; clc; % nacitanie dat zo suboru do matice inpTM inpTM = dlmread('magn_observ.dat',' '); % stlcpe su separovane do jednoriadkovych matic T and M T=inpTM(:,1); M=inpTM(:,2); % pocet nacitanych prvkov v matici T dosadene do matice m m = numel(T) % vykreslenie grafu nacitanych udajov

plot(T,M); % popis osi xlabel('mesiac - poradove cislo','FontSize',10); ylabel('anomalne magneticke pole [nT]','FontSize',10); natanie matc zo sborov (najjednoduchie, ASCII): prklad: natanie anomlnych magnetickch dajov z observatria (vzorkovanch v mesanch priemeroch) clear all, close all; clc; % nacitanie dat zo suboru do matice inpTM inpTM = importdata('magn_observ.dat',' '); % stlcpe su separovane do jednoriadkovych matic T and M T=inpTM(:,1); M=inpTM(:,2); % pocet nacitanych prvkov v matici T dosadene do matice m m = numel(T)

% vykreslenie grafu nacitanych udajov plot(T,M); % popis osi xlabel('mesiac - poradove cislo','FontSize',10); ylabel('anomalne magneticke pole [nT]','FontSize',10); MATLAB (1) - vod do programovania vedeckch problmov, LS 2018 zpis matc do sborov (najjednoduchie, ASCII): vyuitie prkazu dlmwrite() : dlmwrite(nzov sboru,delimiter) presn opak prkazu dlmread(): Pozrite si detaily a prklady v Helpe.

prca so sbormi natavanie a ukladanie dajov natanie matc zo sborov (troku zloitejie): alie monosti (natania textovho sboru): textread, textscan, ... (vi. help Matlabu) natanie XLS sboru prkaz xlsread(): inp = xlsread(fname); kde inp je matica s natanmi dajmi a fname je nzov sboru. Pozor! Maximlny poet natanch riadkov je 65536 a formt je pre Excel 97 a star . prca s binrnymi sbormi prkaz fread():

A = fread(fid) (viacej: help Matlabu) %nata binrne daje zo sboru prca so sbormi natavanie a ukladanie dajov natanie matc zo sborov (sofistikovanejie, ASCII): prkazy fopen(), fscan(), fgetl() a fclose(): (otvorenie sboru, natanie dajov, zatvorenie sboru) Otvorenie sboru: fid = fopen(filename) alebo fid = fopen(filename, permission)

fid skalrna hodnota (file identifier), 1: spen otvorenie 1.sboru, 2: druhho, ... -1: nespen otvorenie sboru permission argument, definujci monosti prce so sborom prca so sbormi natavanie a ukladanie dajov po prkaze fopen() nasleduje samotn natanie prvkov sboru: Bu: tline = fgetl(fid) Alebo:

nata textov riadok ako reazec do premennej tline (podobn je fgets(), ale ten nata aj znak novho riadku /newline character/, funkcia fgetl() tento znak ignoruje) A = fscanf(fid) alebo A = fscanf(fid, format) alebo A = fscanf(fid, format, size) nata formtovan daje zo sboru do matice s definovanmi rozmermi (pozor, natava daje po stpcoch!!!). Detaily ohadom format a size s na alom snmku. prca so sbormi natavanie a ukladanie dajov fscanf()

format: znak percenta a pismenko: %e, %f, %g floating-point numbers %c sequence of characters; number specified by field width %d base 10 integers %i defaults to base 10 integers %o signed octal integer %s a series of non-white-space characters (string) %u signed decimal integer %x signed hexadecimal integer Prklad: [A,count] = fscanf(fid,'%f); % nacita hodnoty ako realne cisla (float) do matice A

[A,count] = fscanf(fid,'%f',[ncols,nrows]); % nacita hodnoty ako realne cisla (float) do matice A, % pricom jej rozmery su dane vopred pomocou nrows a ncols prca so sbormi natavanie a ukladanie dajov fscanf() size: nacitanie do jedno alebo dvojrozmernej matice n alebo [m,n] ! Prklad: [A,count] = fscanf(fid,'%f',[columnsn, rowsm]); nata do matice A hodnoty z otvorenho sboru s identifiktorom

fid ako relne premenn (floating point numbers), priom rozmer matice je dan premennmi rowsm a columnsn . Pozor, kee natanie ide po stpcoch (column order), poradie rowsm a columnsn je prehoden a maticu A bude treba nsledne transponova !!! prca so sbormi natavanie a ukladanie dajov natanie matc zo sborov (sofistikovanejie, ASCII): Zatvorenie sboru: Na zver prce so sborom mus by vdy prkaz fclose(): status = fclose(fid) alebo status = fclose('all')

status: 0 (spen zatvorenie sboru) -1 (nespen zatvorenie sboru) prca so sbormi natavanie a ukladanie dajov pozn.: Pri prci so sbormi je vemi uiton prkaz uigetfile(): [fname,pname] = uigetfile('koncovka','nazov okna'); Sli na na zskanie mena sboru , otvor windovsovsk okno a ukazuje cestu k sboru. Po realizcii ulo v premennej fname nzov sboru a pname cestu k nemu. Prklad: [fname,pname] = uigetfile('*.dat','input of data (ASCII)');

al uiton prkaz msgbox(): msgbox(str,nazov); Sli na zobrazenie uritej informcie (reazec str) v okne. Prklad: s = 'toto okno sluzi na zobrazenie nejakej spravy'; msgbox(s,'message'); prca so sbormi natavanie a ukladanie dajov V prpade ukladania gridov (sieok) z 2D dt existuj rzne protokoly, napr. ESRI (geodzia, kartografia, GIS), VTK (medicna, biolgia), SURFER (geofyzika, geovedy), ... at.

ESRI (ASCII) protokol: ESRI (ASCII) protokol prklad vieho gridu: prca so sbormi natavanie a ukladanie dajov natanie ESRI gridu (ASCII formt) zo sboru do matice: close all; clear all; clc; [fname,pname] = uigetfile('*.grd','input of data (ESRI ASCII *.grid file)'); fid = fopen(fname,'r'); %reading of the ESRI ASCII grid header (ncols, nrows, xllcorner, yllcorner, cellsize, row1 = fgetl(fid); [token,rem] = strtok(row1); ncols = str2double(rem) row2 = fgetl(fid); [token,rem] = strtok(row2); nrows = str2double(rem) row3 = fgetl(fid); [token,rem] = strtok(row3); xllcorner = str2double(rem)

row4 = fgetl(fid); [token,rem] = strtok(row4); yllcorner = str2double(rem) row5 = fgetl(fid); [token,rem] = strtok(row5); cellsize = str2double(rem) row6 = fgetl(fid); [token,rem] = strtok(row6); nodata_value = str2double(rem) %reading of the ESRI ASCII grid data [A,count] = fscanf(fid,'%f',[ncols,nrows]); %fscanf() works in column order - columns field = flipud(A'); %readed matrix A must be transponed because function fscanf() wor % and fliiped up and down, because ESRI starts with rows in upper status = fclose(fid); %generating the x_cor and y_cor vectors x_cor = xllcorner:cellsize:(ncols-1)*cellsize+xllcorner; y_cor = yllcorner:cellsize:(nrows-1)*cellsize+yllcorner; %plotting the contourf() map figure, contourf(x_cor,y_cor,field) %information for the user s = [' size of readed grid: ' int2str(nrows) ' rows x ' int2str(ncols) ' columns'];

msgbox(s,'message'); Vyskajte si skript nacitanie_ESRI_grid.m a s nm natanie a zobrazenie sboru test ESRI1.grd . prca so sbormi natavanie a ukladanie dajov Prklad sbor test ESRI2.grd s dajmi anomlneho gravitanho poa na zem cca 10 x 15 km (severn Nemecko). (tto farebn mapa bola vytvoren inm softvrom GS Surfer) Vyskajte si skript nacitanie_ESRI_grid.m a s nm natanie a zobrazenie sboru test ESRI2.grd . prca so sbormi natavanie a ukladanie dajov

Pre natanie ESRI ASII gridu existuje aj pecilna funkcia arcgridread(nazov.grd') v Mapping toolbox, ale ia tto nemme k dispozcii v tom prstupe cez CVTI server. zpis matc do sborov (sofistikovanejie, ASCII): Op prkazy fopen()(s premission: 'w) a fclose(): ale pracuje sa s prkazom na zpis: fprintf() Prklad: close all; clear all; clc; A = [3; 6; 8; 2; 1; 9; 10; 23; 2; 3; 78; 12; 3; 18; 5; 25]; B = [9; 0; 1; 3; 2; 8; 12; 34; 3; 8; 88; 32; 9; 12; 9; 35]; savearr(:,1) = A; savearr(:,2) = B;

savefile = fopen('check.dat','w'); fprintf(savefile,'%s %s','matica_A','matica_B'); fprintf(savefile, '\n%i %i', savearr'); fclose(savefile); Vyskajte si rzne formty zpisu samotnch hodnt matc: %i %f %e %g %s \n zpis celho sla (integer) zpis relneho sla (float)

zpis relneho sla v exponencilnom tvare (exponential) zpis relneho sla vo veobecnom tvare (general) postupnos znakov bez medzier (reazec = string) znamen, e zakad bude zpis realizovan v novom riadku prca so sbormi natavanie a ukladanie dajov zpis matc do sborov (sofistikovanejie, ASCII): Op prkazy fopen()(s premission: 'w) a fclose(): ale pracuje sa s prkazom na zpis: fprintf() Prklad (zpis hlaviky sboru ESRI ASCII): close all; clear all; clc; ncols=100; nrows=200; xllcorner=500100; yllcorner = 5304000;

cellsize=50; nodata_value=1.7014100000000001E+038; savefile1 = fopen('ESRI_header.grd','w'); fprintf(savefile1,'%s %i','ncols', ncols); fprintf(savefile1,'\n%s %i','nrows', nrows); fprintf(savefile1,'\n%s %f','xllcorner', xllcorner); fprintf(savefile1,'\n%s %f','yllcorner', yllcorner); fprintf(savefile1,'\n%s %f','cellsize', cellsize); fprintf(savefile1,'\n%s %e','nodata_value', nodata_value); fclose(savefile1); Keby sme mali dan pole (2D maticu) na zpis, mohli by sme pokraova prkazom: fprintf(savefile1,%f ',pole); in prklad formtu gridu (Golden Software Surfer)

in prklad formtu gridu (Golden Software Surfer) close all; clear all; clc; rowsm = 0; columnsn = 0; minx = 0; maxx= 0; miny = 0; maxy= 0; minf = 0; maxf [fname,pname] = uigetfile('*.grd','input of data (Surfer ASCII *.grid file)') fid = fopen(fname,'r'); % reading of the GS ASCII grid header (rows, columns, minx, maxx, miny, maxy, tline = fgetl(fid); row1 = tline; %reading the first line - the identificator tline = fgetl(fid); row2 = tline; [token,rem] = strtok(row2); %reading the se columnsn = str2double(token); rowsm = str2double(rem); tline = fgetl(fid); row3 = tline; [token,rem] = strtok(row3); %reading the th minx = str2double(token); maxx = str2double(rem); tline = fgetl(fid); row4 = tline; [token,rem] = strtok(row4); %reading the fo miny = str2double(token); maxy = str2double(rem); tline = fgetl(fid); row5 = tline; [token,rem] = strtok(row5); %reading the th

minf = str2double(token); maxf = str2double(rem); % reading of the main field of the grid - into the matrix A [A,count] = fscanf(fid,'%f',[columnsn, rowsm]); %fscanf() works in column ord status = fclose(fid); field = A'; %readed matrix A must be transponed because function fscanf() wor s = [' size of readed grid: ' int2str(rowsm) ' rows x ' int2str(columnsn) ' c msgbox(s,'message'); figure, contourf(field); colorbar; Zadanie .3: Vyskajte nata daje o vke ternu (relif) zo sboru Helens ESRI ASCII.grd, skste daje vok nejakm spsobom matematicky upravi (umocni, odmocni, vydeli uritm slom,...), prpadne maticu s tmto poom vok preklopi, rotova, . potom zobrazi ako contourf()

a napokon zapsa ako nov sbor vo formte grid ESRI ASCII. Pozn.: Pri tvorbe exportu do formtu grid ESRI ASCII vm me posli aj skript zo snmku . 29 dnenej prednky. Sprvnos exportu matematicky upravench hodnt vok do novho sbor vo formte grid ESRI ASCII si mete skontrolova op pomocou skriptu z dnenej prednky. Zadanie .3: Prklad sprvneho vsledku tohto zadania (pri umocnen pvodnch dajov, bez preklopenia alebo rotcie):

Recently Viewed Presentations

  • Is The Rhythm of Solar Activity Changing?

    Is The Rhythm of Solar Activity Changing?

    Synoptic Solar Monitoring. The parameters we measure should be useful or reflect something useful. They should be easily measurable and calibratable, and not subject to things that can cause significant breaks or discontinuities in the data.
  • Walking Together and Sharing Our Story Luke 24:13-24

    Walking Together and Sharing Our Story Luke 24:13-24

    Yes, and besides all this, it is now the third day since these things took place. Moreover, some women of our group astounded us. They were at the tomb early this morning, and when they did not find his body...
  • American Imperialism and World War I

    American Imperialism and World War I

    Summation of American Imperialism. US intervention showed America's imperialistic attitude in the early 20th century. US pursued and achieved several foreign policy goals. Expanded its access to foreign markets in order to ensure continued growth of the domestic economy. U.S....
  • Nessun titolo diapositiva - twiki.di.uniroma1.it

    Nessun titolo diapositiva - twiki.di.uniroma1.it

    Economia Aziendale 2. Quadro d'insieme Prof. Romano Boni
  • Energy Flow in Ecosystems - Quia

    Energy Flow in Ecosystems - Quia

    Energy Flow in Ecosystems Ch. 2.1 * * * * * * * * Energy Roles An organism's role can be a Producer Consumer Decomposer A. Producers An organism that makes its own food Plants Algae Microorganisms, bacteria Producers are...
  • In The End, Jesus Wins! A Glimpse into

    In The End, Jesus Wins! A Glimpse into

    InThe End, Jesus Wins!. A Glimpse into Glory. I. The . Person. upon the Throne. B. He is Great in Grace. The rainbow symbolizes: Eternity. Perfection. Covenant Promise. The End of the Storm
  • Computer Security: Principles and Practice, 1/e

    Computer Security: Principles and Practice, 1/e

    Chapter 5: Malicious Software EECS710: Information Security Professor Hossein Saiedian Fall 2014
  • 1st Generation Pencil and Tablet 2nd Generation Excel

    1st Generation Pencil and Tablet 2nd Generation Excel

    MBB traced back to the inventory as being in use - until is received in the Utilization Log. ERIC Utilization Log - demonstrates how the MBB. HCE3928 is assigned to JBC C04FEC. ERIC sends Precinct or SRD information to the...